#[non_exhaustive]pub enum Layer {
L2,
L3,
}target_env=ohos, or macOS, or FreeBSD, or OpenBSD, or NetBSD only.Expand description
Represents the OSI layer at which the TUN/TAP interface operates.
This enum determines whether the interface works at the Data Link Layer (L2/TAP) or the Network Layer (L3/TUN).
§Layer 2 (TAP) - Data Link Layer
TAP interfaces operate at the Ethernet frame level (Layer 2), handling complete Ethernet frames including MAC addresses. This is useful for:
- Bridging networks
- Emulating full Ethernet connectivity
- Applications requiring MAC-level control
- Creating virtual switches
TAP mode requires setting a MAC address and can work with protocols like ARP.
Platform availability: Windows, Linux, FreeBSD, macOS, OpenBSD, NetBSD
§Layer 3 (TUN) - Network Layer
TUN interfaces operate at the IP packet level (Layer 3), handling IP packets directly without Ethernet framing. This is the default and most common mode for:
- VPN implementations
- IP tunneling
- Point-to-point connections
- Routing between networks
TUN mode is simpler and more efficient than TAP when Ethernet-level features are not needed.
Platform availability: All platforms
§Examples
Creating a TAP (L2) interface:
use tun_rs::{DeviceBuilder, Layer};
let tap = DeviceBuilder::new()
.name("tap0")
.layer(Layer::L2)
.mac_addr([0x00, 0x11, 0x22, 0x33, 0x44, 0x55])
.build_sync()?;Creating a TUN (L3) interface (default):
use tun_rs::{DeviceBuilder, Layer};
// L3 is the default, explicit specification is optional
let tun = DeviceBuilder::new()
.name("tun0")
.layer(Layer::L3)
.ipv4("10.0.0.1", 24, None)
.build_sync()?;§Platform-Specific Notes
- On macOS, TAP mode uses a pair of
feth(fake ethernet) interfaces - On Windows, TAP mode requires the tap-windows driver
- On Linux, both modes use the kernel TUN/TAP driver
- Android and iOS only support TUN (L3) mode
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
L2
Data Link Layer (Ethernet frames with MAC addresses).
TAP mode operates at Layer 2, handling complete Ethernet frames. Requires a MAC address to be configured.
Available on: Windows, Linux, FreeBSD, macOS, OpenBSD, NetBSD
L3
Network Layer (IP packets).
TUN mode operates at Layer 3, handling IP packets directly. This is the default and most common mode for VPN and tunneling applications.
Available on: All platforms