Overview
TheTunBackend creates a virtual network interface (TUN device) to intercept and process IP packets at the network layer. This enables system-wide transparent DPI bypass without application-specific proxy configuration.
TunBackend
The main TUN backend structure that implements theBackend trait.
Structure
Fields
Atomic flag indicating whether the backend is currently running
Channel sender for graceful shutdown signaling
Current TUN device configuration settings
Handle to the running TUN packet processing task
Methods
new()
Creates a newTunBackend instance.
TunBackend in a stopped state.
Example:
start()
Starts the TUN backend and creates the virtual network interface.Backend configuration including:
engine_config: DPI bypass engine configurationmax_queue_size: Maximum packet queue sizebackend_settings: Must beBackendSettings::Tun(TunSettings)
BackendHandle containing:
shutdown_tx: Channel for triggering shutdownstats: Reference to statistics collectorpipeline: Reference to the DPI bypass pipeline
BackendError::AlreadyRunning- Backend is already runningBackendError::NotSupported- Wrong backend settings type or platform not supportedBackendError::Engine- Engine initialization failed
stop()
Stops the TUN backend and destroys the virtual network interface.BackendError::NotRunning- Backend is not currently running
is_running()
Checks if the backend is currently running.true if the backend is running, false otherwise.
is_supported()
Checks if the TUN backend is supported on this platform.true on macOS, false on other platforms.
Note: Currently only macOS is fully supported. Linux and Windows support is planned.
name()
Returns the backend name."tun"
TunSettings
Configuration settings for the TUN device.Fields
Name of the TUN device to createDefault:
None (auto-generated by OS)Example: "utun3" on macOS, "tun0" on LinuxMaximum Transmission Unit size in bytesDefault:
1500Range: Typically 1280-1500 for IPv4IP address to assign to the TUN interfaceDefault:
"10.0.85.1"Network mask for the TUN interfaceDefault:
"255.255.255.0"Flow Key Parsing
The TUN backend includes IPv4 packet parsing to extract flow information:Supported Protocols
Transmission Control Protocol (protocol 6)Extracts source/destination ports from TCP header
User Datagram Protocol (protocol 17)Extracts source/destination ports from UDP header
Internet Control Message Protocol (protocol 1)Uses port 0 for both source and destination
Example
Current Implementation
The TUN backend currently implements:Flow Cleanup Task
A background task that periodically cleans up expired flows:Features Under Development
- TUN device creation and configuration
- Packet reading from TUN device
- Packet writing to TUN device
- Routing table manipulation
- IPv6 support
- Full packet processing pipeline integration
Platform Support
macOS
Supported platform
- Administrator/root privileges
- Uses
utundevices
Linux
Will require:CAP_NET_ADMINcapability or root/dev/net/tundevice access
Windows
Will require:- TAP-Windows adapter or WinTUN
- Administrator privileges
Example: Basic Usage
Testing
The TUN backend includes a mock device for testing:Security Considerations
- Privilege separation: Drop privileges after TUN device creation
- Input validation: Validate all packets before processing
- Resource limits: Enforce packet queue limits to prevent DoS
- Audit logging: Log all TUN device operations
Performance Considerations
- Packet buffering: Uses configurable queue size (
max_queue_size) - Flow cleanup: Periodic cleanup every 30 seconds
- Async I/O: All operations use tokio async runtime
- Zero-copy: Minimal packet copying where possible
Future Development
Planned enhancements:- Full packet processing: Integrate with engine pipeline
- IPv6 support: Parse and process IPv6 packets
- Platform support: Linux and Windows implementations
- Routing integration: Automatic route table management
- Split tunneling: Selective traffic routing
- DNS interception: Transparent DNS-over-HTTPS proxy