The Network Stack and Tunnel system is the core orchestration layer that binds together the gVisor-based network stack, protocol handlers (TCP/UDP/ICMP), DNS resolver, proxy system, and local services. It manages the lifecycle of the tunnel, processes packets from the TUN device, enforces firewall policies, and coordinates traffic routing through the configured proxies.
For DNS resolution architecture, see DNS Resolution System. For proxy management and selection, see Proxy Management System. For connection reliability and anti-censorship features, see Connection Reliability and Anti-Censorship.
The tunnel system is defined by the Tunnel interface intra/tunnel.go74-119 and its concrete implementation rtunnel intra/tunnel.go121-137 The rtunnel orchestrates all major subsystems: the gVisor network stack, protocol handlers, DNS resolver, proxy provider, and local services.
This diagram maps the high-level rtunnel orchestrator to the specific code entities it manages.
Sources: intra/tunnel.go121-137 intra/tunnel.go155-260 intra/tun2socks.go160-180
The rtunnel struct serves as the central manager for the Intra session:
| Component | Type | Purpose |
|---|---|---|
t | *core.Volatile[tunnel.Tunnel] | Atomic reference to the gVisor netstack tunnel, allowing hot-swap during Restart() intra/tunnel.go125 |
ctx | context.Context | Lifecycle context for all subsystems intra/tunnel.go122 |
handlers | netstack.GConnHandler | Aggregates TCP/UDP/ICMP protocol handlers intra/tunnel.go128 |
proxies | ipn.Proxies | Proxy provider and manager (proxifier) intra/tunnel.go129 |
resolver | dnsx.Resolver | DNS resolution orchestrator intra/tunnel.go130 |
services | rnet.Services | Local services (HTTP/SOCKS servers) intra/tunnel.go131 |
linkmtu | atomic.Int32 | Current link MTU, atomically updated intra/tunnel.go133 |
closed | atomic.Bool | Shutdown flag intra/tunnel.go135 |
Sources: intra/tunnel.go121-137
Tunnel creation follows an 8-second timeout bootstrap sequence defined by mktunTimeout intra/tunnel.go51 The NewTunnel2 function handles the orchestration of these components, while Connect variants provide simplified entry points for external callers intra/tun2socks.go160-180
Sources: intra/tunnel.go159-224 intra/tun2socks.go160-180
The initialization sequence includes:
core.EitherOr and mktunTimeout intra/tunnel.go175-190x64.NewNatPt2 for ALG/NAT64 translation intra/tunnel.go194ipn.NewProxifier with dual-stack support intra/tunnel.go195dtr.kickstart(proxies) intra/tunnel.go205-208dnsx.NewResolver and registers various transports intra/tunnel.go212-224tunnel.NewGTunnel tunnel/tunnel.go173-221For details, see Tunnel Management and Lifecycle.
The tunnel leverages gVisor's user-space network stack to process IP packets from a TUN device.
This diagram maps the packet processing path from the physical device to the protocol handlers.
Sources: tunnel/tunnel.go173-204 intra/netstack/netstack.go30-58
endpoint implementation allows the network stack to remain active while underlying link details change tunnel/tunnel.go71netstack.Up attaches the endpoint to a NIC and configures spoofing and promiscuous mode intra/netstack/netstack.go30-88netstack.Route sets the routing table for the stack, typically covering all IPv4/IPv6 traffic via header.IPv4EmptySubnet and header.IPv6EmptySubnet intra/netstack/netstack.go175-209The system provides comprehensive statistics via the Stat() method, which aggregates data from the network stack.
core.Barrier with a 30-second TTL (statttl) to reduce overhead intra/tunnel.go126 intra/tunnel.go139Stat function in gtunnel extracts counters from the gVisor stack and maps them to Intra's NetStat structure tunnel/tunnel.go65Sources: intra/tunnel.go121-139 tunnel/tunnel.go65
The tunnel uses specialized handlers for different L4 protocols:
OutboundTCP which provides the stack.TCPForwarder callback intra/netstack/netstack.go55OutboundUDP which provides the stack.UDPForwarder callback intra/netstack/netstack.go56OutboundICMP which provides the stack.ICMPForwarder callback intra/netstack/netstack.go57For details, see Protocol Handlers (TCP/UDP/ICMP).
Global settings control the behavior of the network stack and tunnel:
pcapsink. Controlled via SetPcap which manages the pcapio sink tunnel/tunnel.go63 tunnel/tunnel.go223-241Mtu() tunnel/tunnel.go87-93 and SetLinkMtu() intra/tunnel.go101intra/settings/config.go control behaviors like HappyEyeballs, PortForward, and EndpointIndependentMapping intra/settings/config.go33-51For details, see Runtime Configuration and Settings.
Sources: