Skip to content

Policy routes by domain, hardware offload kept (#36) - #62

Merged
Cd1s merged 1 commit into
mainfrom
feat/policy-route-domains
Sep 25, 2026
Merged

Cd1s merged 1 commit into
mainfrom
feat/policy-route-domains

Conversation

@Cd1s

@Cd1s Cd1s commented Sep 25, 2026

Copy link
Copy Markdown
Owner

What

  • policy_routes[] accept domains: [example.com, …] and domains_file: /abs/path next to mac / src / dst (all conditions must match; a domain covers its subdomains).
  • dnsmasq — the main instance and the proxy module's second one (which skips names it answers with fake-ip) — puts every address from upstream answers into the policy's nft sets pr_<i>_4 / pr_<i>_6 (nftset=). The policy rule marks only a NEW connection's first packet with the WAN's fwmark; the connection keeps its WAN through the ct mark and goes into the flowtable (PPE / WED) like any other — line rate stays.
  • Learned addresses leave a set after a day without new connections; each new connection restarts the timer (re-adding by dnsmasq does not refresh it — checked in the 6.18 source). Every firewall reload rebuilds the table and refills the sets with what they held and their remaining time, unless the domain list changed (then the set starts empty, so a removed domain does not stay on the WAN).
  • Validation: DNS names only, absolute domains_file, a bad line reported by line number, empty lists refused; at most 16 domain policies (keeps each nftset= line under dnsmasq's 1024 bytes). An empty set keeps its rule (never degrades into "match every destination").
  • No new process. The image switches to Alpine's dnsmasq-dnssec-nftset (adds nettle, ~0.65 MB installed); install.sh swaps to it where Alpine has it (3.20+). Web UI: a 目标域名 column on 策略路由. Docs: net / dns / proxy / platform, the skill cheat sheet.

Why

Choosing a WAN by domain usually means sending every packet through a userspace proxy or marking every packet in software — both lose the hardware offload that carries normal traffic at line rate. Marking only the first packet keeps the offload.

Verification

  • Go tests: rendering (sets, rules, dnsmasq lines for both instances), validation, file errors, 300-domain lists, the reload carry-over, and that a config without domain policies renders nothing new.
  • CI: the lab config (two domain policies) loads into real nft, both dnsmasq configs pass dnsmasq --test; new end-to-end test in tools/ci.d/net.sh with three network namespaces and a real unprivileged dnsmasq: the learned domain's traffic leaves via wan2, other traffic via wan; addresses survive a reload; a new connection restarts the timer; a changed list starts empty.
  • tools/ci.sh passes (private and public trees).
  • Note: a router running an image with plain dnsmasq (no-nftset) needs the new image before configuring domains:; otherwise dnsmasq refuses the config and the apply rolls back.

Follow-ups (to be filed): IPv6 source prefix vs. the chosen WAN on dual-PD setups; explicit first-match / last-match order of policy routes; learned-address counts in the UI.

Closes #36

https://claude.ai/code/session_01HWeUjo1dmS7gBkCYHrVjCr

…fwmark)

policy_routes entries accept `domains:` / `domains_file:` (subdomains included) next to mac / src /
dst. dnsmasq (both instances; the proxy's skips its fake-ip names) puts upstream answers into
per-policy nft sets; only a NEW connection's first packet is matched and marked, after which the
connection keeps its WAN through the ct mark and goes into the flowtable (PPE / WED) like any other.
Learned addresses expire after a day without new connections (a new connection restarts the timer);
firewall reloads carry them over unless the domain list changed. No new process. The image switches
to Alpine's dnsmasq-dnssec-nftset (+nettle, ~0.65 MB); install.sh too.

Tests: rendering, validation, files, the carry-over; CI: real nft + dnsmasq --test, and an end-to-end
test in three network namespaces with a real dnsmasq.

Closes #36
@Cd1s
Cd1s merged commit f2fb2b2 into main Sep 25, 2026
2 checks passed
@Cd1s
Cd1s deleted the feat/policy-route-domains branch September 25, 2026 17:59
@Cd1s Cd1s mentioned this pull request Sep 25, 2026
52 tasks done
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.

提议:按域名选 WAN 也保持硬件线速(dnsmasq nftset + fwmark)

1 participant