Skip to content

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).

Crateours-mod-tun, modules/protocols/tun
Binary featuretun
Crate featuresnone

requires ​

Nothing.

provides ​

  • Provide::Inbound { key: "tun", decode: config::decode } — its own decoder: the UDP NAT is the nat block, only udp.timeout is taken from listener; fields removed in sing-box and Android fields are rejected with a hint.
  • Provide::Check(config::check) — auto_redirect on no more than one inbound, not together with auto_nft and without dial.fwmark on the outbounds. route.default_mark is 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 (table 2022, rules from 9000, the marks 0x2023/0x2024), the default DNS address, the uid ranges that routing lets through (ExcludedRanges).
  • src/lib.rs — Tun: the first start creates the device (IFF_MULTI_QUEUE), the stack's listeners, the routing and auto_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 for auto_redirect, the binary sets it through Instance::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 lives udp_timeout without packets, and after an RST or a FIN from both sides — 10 s, as in sing-tun); UDP is read from the device into a UdpNat, and replies are assembled into packets (src/packet.rs); ICMP echo is answered in place; connections to loopback_address are returned to the sender. DNS to the device's addresses is marked with the dns protocol, and the dispatcher answers it as hijack-dns before the rules.
  • src/route.rs — the table's routes (BuildAutoRouteRanges, subtracting route_exclude_address) and the ip rule rules in sing-tun's order; at start, rules left at the same priorities by a previous process are removed.
  • src/redirect.rs — the inet ours-tun table (Plan → nft::Batch), local routes to 127.0.0.1/::1 on every interface for sockets with SO_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_mark are 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 tun0 without a gateway.
  • No batched reading and no offload: run does a read/write per packet, the device is opened without IFF_VNET_HDR, TcpNat is one Mutex for 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).

GPL-3.0-or-later