English
ours-auto-nft
crates/auto-nft is the tproxy inbound's auto_nft: a table that sends to tproxy what the process must see, policy routing that delivers it to a local socket, and removal of conntrack entries that became stale after the capture set changed.
Design
- Everything is written over netlink through
ours-net-info: nf_tables batches (ours_net_info::nft), rtnetlink rules and routes (ours_net_info::policy), ctnetlink (ours_net_info::conntrack). Thenft,ip,conntrackprograms are not run. table.rs— theinet ourstable (TABLE): the decision is made on the first packet of a flow and stored inct mark; every following packet of a captured flow takes the firstpreroutingrule, any other returns at the second. The first packet is looked up once in@capture4/@capture6— elementssource . destination . protocol . port(ranges).Plan— the marks (own_mark,router_mark) and parameters;Element— a set element.AutoNft::apply(plan, elements, forget_flows)— the first call, or a call with a different plan, replaces the whole table and writes the routing; otherwise only the capture sets change. Withforget_flowsthe flows that went past the process and are now captured are forgotten.remove— remove the table and the routing;is_applied.clean(plan)— remove the table (whoever wrote it) and the routing: what was left after a crashed process.
Who uses it
Only the binary: app/ours/src/auto_nft.rs (the auto-nft feature, pulls in tproxy and router); without the feature — app/ours/src/auto_nft_disabled.rs.
Why not a module
- The table belongs to the process, not to one configuration, and the set needs both the tproxy options (
ours-mod-tproxy) and the router's rules (ours_mod_router::bypass). So the binary owns it. - The binary writes the capture set the running configuration's rules ask for at start, after a reload and after a rule-set update. It removes the set on exit, including on an error after start (
app/ours/src/auto_nft.rs). cleanremoves what a crashed process left: onours cleanand atrunstart (clean_left). At start it runs only if theinet ourstable is in place, since routing table 100 may belong to the host, and only if no other ours process runs in the network namespace.app/ours/src/leftovers.rsfinds such a process: everyrunholds the abstract Unix socketours/run/<pid>, which the kernel closes however the process dies, and/proc/net/unixshows only the sockets of its own namespace.
Tests
crates/auto-nft/tests/netns.rs — the table as the kernel holds it (nft list table), against the reference in crates/auto-nft/tests/data/. The tests change netfilter, so they run only in their own network namespace and with OURS_NETNS_TESTS=1; the command is in the netns tests.
The end-to-end scenario without root is tests/e2e/netns/auto-nft.sh, see E2E.