TUN — mihomo
mihomo's tun: is a top-level config block (a listeners: entry with type: tun takes the same keys — see Notes). The schema is nearly identical to sing-box's TUN inbound (mihomo's implementation is derived from sing-box's) with a few mihomo-specific additions: explicit dns-hijack, ICMP-drop toggle, and Darwin recvmsg_x/sendmsg_x optimization flags.
Top-level tun: block
| Field | Type | Default | Allowed values | Description |
|---|---|---|---|---|
enable | bool | false | true | false | Master switch. When false the rest of the block is ignored. |
device | string | (auto) | <interface name> | TUN device name. |
stack | C.TUNStack | mips | system | gvisor | mixed | mips | TCP/IP stack. The sing-box values plus mips (mihomo's own userspace stack), which is the default. |
dns-mode | string | (hijack with auto-route on Windows / Linux, else disabled) | disabled | native | hijack | System DNS handling. native makes the TUN interface the system DNS server; hijack does the same and also intercepts port-53 traffic sent to other interfaces (Windows needs strict-route, Linux needs auto-redirect); disabled leaves the system DNS settings alone. When empty, Windows and Linux use hijack if auto-route is on and disabled otherwise; macOS uses disabled. Any other value fails at startup. |
dns-hijack | []string | [0.0.0.0:53] | [<address>] | Addresses that should be hijacked as DNS — outgoing UDP to these endpoints is redirected to the internal DNS engine. The default 0.0.0.0:53 (also written any:53) catches every port-53 query. |
auto-route | bool | true | true | false | Auto-install OS routes. |
auto-detect-interface | bool | true | true | false | Auto-discover the default outbound interface — used for excluding TUN-bound traffic from re-entering TUN. |
mtu | uint32 | 0 (auto) | <bytes> | Device MTU. 0 picks a sensible default (9000 on most platforms). |
gso | bool | false | true | false | Generic Segmentation Offload (Linux). Improves throughput when the kernel supports it. |
gso-max-size | uint32 | 65536 | <bytes> | Maximum segment size when GSO is active. |
inet6-address | []netip.Prefix | [fdfe:dcba:9876::1/126] | <CIDR> | IPv6 addresses to assign to the device. There is no inet4-address key here (it is commented out at config/config.go:285) — see Notes for the IPv4 address. |
iproute2-table-index | int | 2022 | <int> | Linux iproute2 table index. |
iproute2-rule-index | int | 9000 | <int> | Linux iproute2 rule index. |
auto-redirect | bool | false | true | false | Linux NFTables auto-redirect (faster TCP path). Requires auto-route: true — startup fails otherwise. |
auto-redirect-input-mark | uint32 | 0 | <uint32> | fwmark on TUN-bound packets. |
auto-redirect-output-mark | uint32 | 0 | <uint32> | fwmark on TUN-egress packets. |
auto-redirect-iproute2-fallback-rule-index | int | 0 | <int> | Fallback rule index when auto-redirect can't install. |
loopback-address | []netip.Addr | [] | <IP> | Addresses treated as loopback. |
strict-route | bool | false | true | false | Block traffic that would otherwise bypass TUN (kill-switch semantics). |
route-address | []netip.Prefix | [] | <CIDR> | Route only these CIDRs through TUN when auto-route is on. |
route-address-set | []string | [] | <rule-provider name> | Rule-provider-driven inclusion. |
route-exclude-address | []netip.Prefix | [] | <CIDR> | CIDRs to keep on the default interface. |
route-exclude-address-set | []string | [] | <rule-provider name> | Rule-provider-driven exclusion. |
include-interface | []string | [] | <interface> | Only attach to these interfaces. |
exclude-interface | []string | [] | <interface> | Exclude these interfaces. |
include-uid | []uint32 | [] | <uid> | UID inclusion. |
include-uid-range | []string | [] | <from:to> | UID range inclusion. |
exclude-uid | []uint32 | [] | <uid> | UID exclusion. |
exclude-uid-range | []string | [] | <from:to> | UID range exclusion. |
exclude-src-port | []uint16 | [] | <port> | Source-port exclusion. |
exclude-src-port-range | []string | [] | <from:to> | Source-port range exclusion. |
exclude-dst-port | []uint16 | [] | <port> | Destination-port exclusion. |
exclude-dst-port-range | []string | [] | <from:to> | Destination-port range exclusion. |
include-android-user | []int | [] | <user id> | Android multi-user inclusion. |
include-package | []string | [] | <package name> | Android package inclusion. |
exclude-package | []string | [] | <package name> | Android package exclusion. |
include-mac-address | []string | [] | <MAC> | MAC-based inclusion. |
exclude-mac-address | []string | [] | <MAC> | MAC-based exclusion. |
endpoint-independent-nat | bool | false | true | false | Enable endpoint-independent NAT for UDP. Required by some games / VoIP apps. |
udp-timeout | int64 | 300 | <seconds> | UDP idle timeout in seconds. |
icmp-timeout | int64 | 0 (10s built-in) | <seconds> | Idle timeout for forwarded ICMP sessions, in seconds. 0 keeps the built-in 10-second default. |
disable-icmp-forwarding | bool | false | true | false | Drop ICMP packets at the TUN layer (mihomo-specific). |
congestion-controller | string | (stack default) | cubic | reno | bbr | bbr3 | TCP congestion controller of the TUN stack. Only takes effect with stack: mips. |
file-descriptor | int | 0 | <fd> | Use a pre-opened file descriptor instead of creating a TUN device. Useful for VPN-service integration where the OS hands you the fd. |
inet4-route-address | []netip.Prefix | [] | <IPv4 CIDR> | Per-family form of route-address; its entries are added to route-address. |
inet6-route-address | []netip.Prefix | [] | <IPv6 CIDR> | Per-family form of route-address; its entries are added to route-address. |
inet4-route-exclude-address | []netip.Prefix | [] | <IPv4 CIDR> | Per-family form of route-exclude-address; its entries are added to route-exclude-address. |
inet6-route-exclude-address | []netip.Prefix | [] | <IPv6 CIDR> | Per-family form of route-exclude-address; its entries are added to route-exclude-address. |
recvmsgx | bool | true | true | false | Darwin only — use the recvmsg_x syscall for batching. |
sendmsgx | bool | false | true | false | Darwin only — use sendmsg_x. |
Source: config/config.go:273-332 · pinned at v1.19.34 (0455d0d)
Stack choice
Same as sing-box, plus mihomo's own mips stack, which is the default:
system— kernel TCP/IP. Fastest, requires kernel TUN support.gvisor— userspace stack. Slower, portable.mixed— system stack for TCP, gVisor for UDP.mips— mihomo's own userspace IP stack (MIPS = mihomo IP stack, not the CPU architecture). Default for the top-leveltun:block and fortype: tunlisteners, and the only stack that appliescongestion-controller.
DNS hijack
dns-hijack is mihomo's idiomatic way to route DNS queries through the internal resolver:
tun:
enable: true
dns-hijack:
- any:53 # All port-53 traffic, TCP and UDP
- 8.8.8.8:53 # Specific server onlyThis is implemented at the TUN layer — DNS packets are intercepted before they reach the routing rules. It's faster than running a UDP inbound on 53 and routing back through the rules.
dns-mode covers the other half — the operating system's DNS settings. native makes the TUN interface the system DNS server, so applications send their queries into TUN; hijack does the same and also intercepts port-53 traffic sent to other interfaces (Windows needs strict-route, Linux needs auto-redirect); disabled leaves the system DNS settings alone. Left empty, Windows and Linux use hijack while auto-route is on and disabled otherwise; macOS uses disabled.
Examples
Desktop transparent proxy:
tun:
enable: true
device: mihomo-tun
stack: mips
mtu: 9000
dns-hijack:
- any:53
auto-route: true
auto-detect-interface: true
strict-route: true
route-address:
- 0.0.0.0/1
- 128.0.0.0/1
- ::/1
- 8000::/1
route-exclude-address:
- 192.168.0.0/16
- 10.0.0.0/8Linux NFTables auto-redirect:
tun:
enable: true
device: utun0
stack: system
dns-hijack: [any:53]
auto-route: true
auto-redirect: true
auto-redirect-input-mark: 0x100
auto-redirect-output-mark: 0x200Android app split — proxy only specific apps:
tun:
enable: true
stack: system
mtu: 9000
dns-hijack: [any:53]
auto-route: true
include-package:
- com.netflix.mediaclient
- com.spotify.music
exclude-package:
- com.google.android.gmsNotes
inet4-addressis commented out in the source (config/config.go:285) — the top-level block derives its IPv4 address fromdns.fake-ip-rangeinstead: the address written there, with a/30prefix (198.18.0.1/30with the default range). IPv6 still uses the explicitinet6-addressfield.dns-hijackaccepts a couple of special forms:any:53(the same as0.0.0.0:53) matches TCP and UDP port 53 to any address.- A
tcp://orudp://prefix is accepted but ignored — both protocols are hijacked either way. - Bare
IP:portfor specific upstream resolvers.
auto-redirectbuilds onauto-routerather than replacing it: enabling it withoutauto-route: truefails at startup (listener/sing_tun/server.go:435-437). On NFTables-capable kernels, add it on top ofauto-routefor faster TCP redirection.- The
recvmsgx/sendmsgxflags are macOS-only optimizations that use the per-syscall batching variants.recvmsgxis on by default;sendmsgxstays off by default because upstream notes it can freeze the kernel during multi-threaded downloads. disable-icmp-forwardingis mihomo-specific — useful when you have a separate ICMP path (e.g. a routing-table-based pinger) and don't want TUN to capture pings.- A
listeners:entry withtype: tuntakes the same keys (withoutenable, plusinet4-address) and also defaults tostack: mipsanddns-hijack: [0.0.0.0:53]. The other defaults in the table above belong to the top-level block only — in a listener,auto-route,auto-detect-interfaceandrecvmsgxare off unless set.
Cross-core notes
- Xray-core has a minimal TUN inbound. See TUN — Xray-core.
- sing-box has the same feature set under
type: "tun"listed ininbounds[]with snake_case names, includingdns_mode(same three values; when unset,hijackwithauto_routeanddisabledotherwise). It has nomipsstack — its default ismixed, orsystemin builds without gVisor. See TUN — sing-box.
Source: config/config.go:273-332 · v1.19.34 (0455d0d)
