Skip to content

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). The nft, ip, conntrack programs are not run.
  • table.rs — the inet ours table (TABLE): the decision is made on the first packet of a flow and stored in ct mark; every following packet of a captured flow takes the first prerouting rule, any other returns at the second. The first packet is looked up once in @capture4/@capture6 — elements source . 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. With forget_flows the 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).
  • clean removes what a crashed process left: on ours clean and at run start (clean_left). At start it runs only if the inet ours table 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.rs finds such a process: every run holds the abstract Unix socket ours/run/<pid>, which the kernel closes however the process dies, and /proc/net/unix shows 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.

GPL-3.0-or-later