TUN Mode (Full-Tunnel VPN)
TUN mode provides a full-tunnel VPN experience where all client traffic is routed through the server. This differs from SOCKS5 relay mode, which requires per-application SOCKS5 configuration.
TUN Mode vs SOCKS5 Mode
Section titled “TUN Mode vs SOCKS5 Mode”| Feature | TUN Mode | SOCKS5 Mode |
|---|---|---|
| Traffic routing | Full tunnel, all apps | Per-app, requires SOCKS5 config |
| Setup complexity | Server-side NAT config | Client-side app configuration |
| Performance | Kernel-level packet handling | Application-level relay |
| Use case | Complete privacy, all traffic | App-specific tunneling |
In TUN mode, the client creates a virtual TUN interface and routes all traffic through it. The server receives these packets and NATs them to the internet, similar to a traditional VPN.
Egress Containment and NAT Requirements
Section titled “Egress Containment and NAT Requirements”TUN startup is fail-closed. The server derives the network CIDR from tun_ip, blocks private, loopback, CGNAT, NAT64, documentation, and other special-use destinations, and drops all TUN-originated traffic to the server host. The same destination policy applies to raw TUN packets, TCP, UDP, and DNS. DNS responses with mixed answers retain only public addresses; answers with no public addresses return NODATA.
nat_enabled = false disables NAT only; it does not disable containment. Setup failures stop TUN startup before clients are accepted.
From v1.3.9 each client can only send from its own assigned tunnel address, so one client can’t pose as another. Two opt-in settings in [server.network] relax containment for raw TUN traffic, both off by default:
allow_client_to_client = truelets clients on this server reach each other’s tunnel addresses. It covers this server’s own TUN pool only.allowed_internal_ranges = [...]lists extra internal ranges clients may reach, such as a home LAN.
The server’s own addresses stay unreachable either way, and traffic from the internet still can’t reach a client: rVPN has no port forwarding. See Server Config for the full rules.
Enable IP forwarding and install both iptables and ip6tables:
sudo sysctl -w net.ipv4.ip_forward=1echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.confAt startup the server reconciles only its RVPN_INPUT, RVPN_FORWARD, and RVPN_NAT chains, inserts TUN-specific jumps ahead of broad existing accepts, and adds MASQUERADE for the normalized TUN subnet when NAT is enabled. It does not flush global rules or change chain policies. Do not add a parallel nftables ruleset; on nft-backed distributions use the distribution’s iptables/ip6tables frontend.
macOS does not support server-side TUN mode natively. For macOS servers, use SOCKS5 mode or run rvpn-server inside a FreeBSD/Linux VM with proper NAT configuration.
FreeBSD
Section titled “FreeBSD”Enable forwarding and PF. The active main ruleset must contain both attachment points, placed before broad pass quick rules that could otherwise bypass the anchor:
nat-anchor "rvpn"anchor "rvpn"Have the operator validate and load the main ruleset after adding those lines. The installer and server never enable PF, alter /etc/pf.conf, reload the main ruleset, flush PF state, or use ipfw/natd. At TUN startup the server writes /usr/local/etc/rvpn/rvpn.anchor.conf, then runs only the scoped operations:
pfctl -a rvpn -nf /usr/local/etc/rvpn/rvpn.anchor.confpfctl -a rvpn -f /usr/local/etc/rvpn/rvpn.anchor.confThe anchor quick-blocks special-use destinations and the host’s own interface addresses ((self)), statefully passes permitted public flows from the TUN subnet, blocks remaining TUN input, and optionally NATs only that subnet through the detected external interface using ($ext_if). TUN startup fails if PF or forwarding is disabled, either attachment is missing, or validation/loading fails.
If a broad pass quick rule appears earlier in the main ruleset than anchor "rvpn", packets it matches never reach the anchor: the startup log warns about this, and for those packets only the userspace destination check (is_blocked_egress in permitted_tun_destination, which also covers the server’s own addresses) still applies. Move the anchor before such rules to restore full kernel-side containment.
Server Configuration for TUN Mode
Section titled “Server Configuration for TUN Mode”[server]bind_address = "0.0.0.0:443"tls_cert_file = "/etc/letsencrypt/live/your-domain.com/fullchain.pem"tls_key_file = "/etc/letsencrypt/live/your-domain.com/privkey.pem"identity_key_file = "/etc/rvpn/server_identity.key"websocket_path = "/api/v1/ws"
[server.network]nat_enabled = true
[server.tun]enabled = truetun_ip = "10.200.0.1/24"mtu = 1420dns_servers = ["1.1.1.1", "8.8.8.8"]Key settings for TUN mode:
websocket_path— Must be/api/v1/wsfor TUN mode clients to reach the TUN endpoint at/api/v1/ws/tun[server.network].nat_enabled— Controls server-managed NAT only; containment remains enabled when this isfalse.[server.tun].tun_ip— The gateway address and prefix. Client allocation, routing, filtering, and NAT all use its normalized network CIDR. Change this if10.200.0.0/24clashes with your LAN.[server.tun].dns_servers— DNS resolvers pushed to clients over the tunnel
Running the Server in TUN Mode
Section titled “Running the Server in TUN Mode”Direct Execution
Section titled “Direct Execution”sudo rvpn-server -c /etc/rvpn/server.tomlAs a systemd Service
Section titled “As a systemd Service”The service runs the same binary regardless of mode. Ensure server.toml has TUN-mode settings as shown above.
sudo systemctl restart rvpn-serverVerification Steps
Section titled “Verification Steps”Check server logs:
sudo journalctl -u rvpn-server -fLook for entries showing the WebSocket path and TUN handler:
INFO Starting rVPN Server on 0.0.0.0:443INFO Server listening on wss://0.0.0.0:443INFO WebSocket endpoint (mobile TUN): /api/v1/ws/tunINFO TUN server started on 10.200.0.1/24Verify the owned rules are active (Linux):
sudo iptables -L RVPN_INPUT -vsudo iptables -L RVPN_FORWARD -vsudo iptables -t nat -L RVPN_NAT -vTest connectivity and containment:
Connect a client in TUN mode and verify:
- Client receives an IP from the normalized
tun_ipsubnet (e.g.,10.200.0.x). - Public IP TCP, UDP, DNS, and ICMP traffic works.
- Server-host, private LAN, loopback, CGNAT, NAT64, and malformed-packet attempts are dropped without disconnecting the session.
- DNS names with mixed private/public answers return only public addresses; all-private answers return NODATA.
Check active connections on server:
sudo ss -tlnp | grep 443sudo ip addr show tun0 # if interface existsTroubleshooting
Section titled “Troubleshooting”Client Cannot Connect
Section titled “Client Cannot Connect”- Verify port 443 is open in firewall
- Check
websocket_pathis/api/v1/ws(clients append/tunautomatically) - Review server logs for TLS or WebSocket upgrade errors
Traffic Flows Out But Nothing Returns
Section titled “Traffic Flows Out But Nothing Returns”- Verify IP forwarding is enabled:
sysctl net.ipv4.ip_forward - Check the owned NAT chain:
iptables -t nat -L RVPN_NAT - Ensure the server’s security group/firewall allows outbound traffic on all ports
No Internet Access on Client
Section titled “No Internet Access on Client”- Confirm
nat_enabled = truein server.toml - Verify the normalized
tun_ipsubnet does not conflict with existing networks - Check that
dns_serversare reachable from the server
TUN Interface on Server
Section titled “TUN Interface on Server”The server creates a real TUN interface (e.g., tun0 with IP 10.200.0.1) when TUN mode is enabled. This provides true TUN-to-TUN tunneling:
| Component | Description |
|---|---|
| Server TUN | tun0 with IP 10.200.0.1/24 |
| Client TUN | Virtual interface with IP from 10.200.0.x |
| Routing | Kernel routes packets through TUN interface |
The server’s TUN interface handles:
- Writing packets received from clients to the kernel
- Reading response packets from the kernel
- Forwarding responses back to the appropriate client
Reverse Proxy Considerations
Section titled “Reverse Proxy Considerations”When running behind a reverse proxy (Caddy, nginx, HAProxy), ensure the proxy forwards /api/v1/ws/tun to the server. The proxy configuration for TUN mode is identical to SOCKS5 mode — both use the same WebSocket base path.
See Reverse Proxy Setup for complete proxy configurations.
Quick Reference
Section titled “Quick Reference”| Item | Value |
|---|---|
| WebSocket path | /api/v1/ws |
| TUN endpoint | /api/v1/ws/tun |
| Default port | 443 |
| DHCP range default | 10.200.0.0/24 |
| NAT required | Yes |