English
tun
The tun inbound: a Linux TUN device with the system stack, auto_route and auto_redirect to the extent of sing-box 1.14 (sing-tun: tun_linux.go, stack_system.go, redirect_nftables*.go).
| Crate | ours-mod-tun, modules/protocols/tun |
| Binary feature | tun |
| Crate features | none |
requires
Nothing.
provides
Provide::Inbound { key: "tun", decode: config::decode }— its own decoder: the UDP NAT is thenatblock, onlyudp.timeoutis taken fromlistener; fields removed in sing-box and Android fields are rejected with a hint.Provide::Check(config::check)—auto_redirecton no more than one inbound, not together withauto_nftand withoutdial.fwmarkon the outbounds.route.default_markis checked at build time.Provide::Schema { name: "inbounds.tun" }.
Syntax: tun.
Dependencies
ours-tun-sys (the device's ioctls), ours-net-info (rtnetlink: addresses, routes, ip rule; nf_tables), ours-net-sys (SO_ORIGINAL_DST, sockets), ours-relay (UdpNat), ours-api, ours-codec, ours-json, ours-bufpool.
Design
src/settings.rs—Settings: the checked fields, sing-tun's defaults (table2022, rules from9000, the marks0x2023/0x2024), the default DNS address, the uid ranges that routing lets through (ExcludedRanges).src/lib.rs—Tun: the firststartcreates the device (IFF_MULTI_QUEUE), the stack's listeners, the routing andauto_redirect; each shard reads its own queue. The device's routing is held by one inbound: on reload the successor opens the same device and takes it over, and the replaced inbound does not remove it (owners).own_mark— the mark of the instance's sockets forauto_redirect, the binary sets it throughInstance::own_mark. The owners are one for the process: the table and the rules are the host's.src/stack.rs— the system stack: TCP is rewritten to a kernel listener on the device's address from the address following it, and back (src/nat.rs— the port table; an entry livesudp_timeoutwithout packets, and after an RST or a FIN from both sides — 10 s, as in sing-tun); UDP is read from the device into aUdpNat, and replies are assembled into packets (src/packet.rs); ICMP echo is answered in place; connections toloopback_addressare returned to the sender. DNS to the device's addresses is marked with thednsprotocol, and the dispatcher answers it ashijack-dnsbefore the rules.src/route.rs— the table's routes (BuildAutoRouteRanges, subtractingroute_exclude_address) and theip rulerules in sing-tun's order; at start, rules left at the same priorities by a previous process are removed.src/redirect.rs— theinet ours-tuntable (Plan→nft::Batch), local routes to127.0.0.1/::1on every interface for sockets withSO_BINDTODEVICE(a priority 1 rule), OpenWrt fw4 rules. The host address sets and the local routes are reconciled every 5 s.
Differences from sing-tun
- No pre-matching through nfqueue (
auto_redirect_nfqueue,auto_redirect_reset_markare accepted): a refusal by a rule happens after the handshake. - uid ranges in nftables sets are compared in network byte order (
byteorder), while sing-tun uses host order, which is wrong for ranges on little-endian. - The table's route is
dev tun0without a gateway. - No batched reading and no offload:
rundoes aread/writeper packet, the device is opened withoutIFF_VNET_HDR,TcpNatis oneMutexfor all shards; see the performance limitations.
Tests
Unit tests: src/packet.rs, src/nat.rs, src/settings.rs, src/route.rs (the rules are checked against sing-box 1.14.4's ip rule), src/config.rs. tests/netns.rs — the nftables table and the rules in the kernel, checked against tests/data/ (see how to run it in a separate netns). The end-to-end scenario without root — tests/e2e/netns/tun.sh (PROXY=ours and PROXY=sing-box).