Skip to main content

Layer

Enum Layer 

Source
#[non_exhaustive]
pub enum Layer { L2, L3, }
Available on Windows, or Linux and non-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).

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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

L2

Available on Windows or Linux or FreeBSD or macOS or OpenBSD or NetBSD only.

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

Trait Implementations§

Source§

impl Clone for Layer

Source§

fn clone(&self) -> Layer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Layer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Layer

Source§

fn default() -> Layer

Returns the “default value” for a type. Read more
Source§

impl From<Layer> for c_short

Available on Linux and non-target_env=ohos only.
Source§

fn from(layer: Layer) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Layer

Source§

fn eq(&self, other: &Layer) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Layer

Source§

impl Eq for Layer

Source§

impl StructuralPartialEq for Layer

Auto Trait Implementations§

§

impl Freeze for Layer

§

impl RefUnwindSafe for Layer

§

impl Send for Layer

§

impl Sync for Layer

§

impl Unpin for Layer

§

impl UnwindSafe for Layer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.