Skip to content

tun (inbound only) ​

A Linux TUN device: everything the system routes into it reaches the rules as ordinary connections. With auto_route the process sets up routes and ip rule by itself, and with auto_redirect also an nftables table that steers TCP past the device straight to a local socket. Compatible with sing-box 1.14 tun on Linux: the same fields, the same table, rule and mark numbers.

It needs CAP_NET_ADMIN; auto_redirect needs nftables in the kernel (nf_tables, nft_redir, nft_nat, nft_reject_inet). The ip and nft binaries are not run: everything is written over netlink.

Fields ​

FieldTypeDefaultMeaning
interface_namestringtunN, N one more than the largestThe device name
addresslist of prefixesrequiredThe device's addresses, IPv4 and/or IPv6, for example 172.19.0.1/30
mtunumber65535The device's MTU
auto_routeboolfalseA default route through the device: the iproute2_table_index table and rules
strict_routeboolfalseA family without an address is unreachable; with auto_redirect, the same for redirection
route_addresslist of prefixesthe whole address rangeWhat to route into the device instead of the default route
route_exclude_addresslist of prefixes—What to exclude from the device's routes
include_interfacelist—Intercept only traffic that came from these interfaces; together with exclude_interface it is an error
exclude_interfacelist—Do not intercept traffic from these interfaces
include_uid, include_uid_rangenumber / string from:to—Intercept only the traffic of these users
exclude_uid, exclude_uid_rangenumber / string from:to—Do not intercept the traffic of these users
include_mac_address, exclude_mac_addresslist of MACs—Only with auto_redirect: intercept (do not intercept) by the sender's MAC
loopback_addresslist of addresses—TCP to this address connects back to the sender (like SideStore/StosVPN)
dns_modedisabled, native, hijackhijack with auto_route, otherwise disabledDNS interception, see below
dns_addresslist of addressesthe one after the first addressThe device's DNS server
iproute2_table_indexnumber2022The auto_route route table
iproute2_rule_indexnumber9000The first priority of the auto_route rules; priorities up to +10 are taken
auto_redirectboolfalsenftables redirection; requires auto_route
auto_redirect_input_markmark0x2023The mark by which UDP and ICMP go to the device's table
auto_redirect_output_markmark0x2024The mark of the process's own sockets
auto_redirect_iproute2_fallback_rule_indexnumber32768A rule after main and default: to the device's table if there are no other routes
auto_redirect_reset_mark, auto_redirect_nfqueuemark, number0x2025, 100Accepted; there is no pre-matching through nfqueue
exclude_mptcpboolfalseMPTCP bypasses the proxy; by default it is dropped
natblocksee tproxyUDP NAT: mapping, filtering, max_flows
stacksystem, gvisor, mixedsystemAccepted; the stack is always the system one

The lifetime of a UDP flow without packets is udp.timeout of the listener block (5m by default; the sing-box udp_timeout); other listener fields are an error for tun. Marks are written as a number or a string ("0x2023"). Deprecated sing-box fields (inet4_address and the other inet4_*/inet6_*, gso, endpoint_independent_nat) are an error with a hint; Android fields (include_package, exclude_package, include_android_user) and platform are an error.

How traffic reaches the rules ​

A system stack, as in sing-tun: TCP from the device is rewritten to a kernel listener on the device's address, UDP is read and answered by the process itself, and ICMP echo is answered on the spot. The inbound in the rules is inbound: <name>, of type tun; the source and destination are the original ones.

auto_route. A default route (or route_address without route_exclude_address) in the iproute2_table_index table and rules starting from priority iproute2_rule_index: the host's own traffic without a bound address and transit traffic go into the device, more specific main routes (except DNS to port 53) do not. The process's own connections must not get back in; that needs route.auto_detect_interface: true (or auto_redirect, which marks the sockets by itself).

auto_redirect. The nftables table inet ours-tun: TCP from the intercepted interfaces and from the host itself is redirected (redirect) to a local listener, while UDP and ICMP are marked with auto_redirect_input_mark and go into the device. All of the process's sockets get auto_redirect_output_mark, so route.default_mark and the outbounds' dial.fwmark together with auto_redirect are an error; so is auto_nft on tproxy. The host's addresses in the table are updated every 5 seconds. On OpenWrt (where there is fw4) the rules that let the device's traffic through are written to /etc/nftables.d with fw4 reload.

DNS. With dns_mode: hijack (and native), DNS to dns_address — by default the address after the device's address (172.19.0.2 for 172.19.0.1/30) — is answered as the hijack-dns action before all rules. If dns_address is set, such DNS is ordinary and needs its own hijack-dns in the rules. With auto_redirect and hijack, DNS to port 53 to any server from the intercepted interfaces and from the host itself is redirected to this address. native does not configure systemd-resolved.

On stop the routes, rules and the nftables table are removed, and the device disappears. A reload with a changed inbound picks up the device without interruption. If the process is killed (SIGKILL, OOM), the kernel removes the device with its routes, but the inet ours-tun table and the rules stay and, with auto_redirect, steer all of the host's TCP into a closed port. The next ours run or ours clean removes them; see running as a service.

Performance limitations ​

tun moves packets one at a time and without offload, so it costs more CPU than sing-tun on Linux. It shows on routers forwarding at high rates, on weak CPUs, with many TCP connections, and with dense UDP. To avoid it:

  • auto_redirect: true: TCP bypasses the device, only UDP and ICMP go through it;
  • on a router, the tproxy (TCP and UDP) or redirect (TCP only) inbound instead of tun;
  • do not lower mtu without need.

Not supported ​

netns, route_address_set, route_exclude_address_set — not implemented yet. Pre-matching of auto_redirect through nfqueue and rules for Docker are not set up.

Example ​

yaml
inbounds:
  tun-in:
    tun:
      interface_name: tun0
      address: [172.19.0.1/30, "fdfe:dcba:9876::1/126"]
      auto_route: true
      auto_redirect: true
      strict_route: true
      route_exclude_address: [192.168.0.0/16]
outbounds:
  direct:
    direct:
  proxy:
    socks:
      server: 192.0.2.10:1080
dns:
  upstreams:
    local:
      udp: 1.1.1.1
  final:
    upstream: local
route:
  auto_detect_interface: true
  rules:
    - match:
        ip_is_private: true
      to:
        outbound: direct
  final:
    outbound: proxy

GPL-3.0-or-later