Below is a “tour” of the Rust program in plain language.
Think of it in three layers:
• What the program tries to accomplish
• How each Rust function helps do that job
• The meaning of the individual lines / symbols you see in the code
1. Big picture (―30 sec overview)
Ask Linux to create a TAP virtual network card called tap0.
Give that card an IP address (192.168.1.1/24) and bring it up.
Add a routing rule so packets for 192.168.1.0/24 leave through tap0.
Exit.
TAP = layer-2 (Ethernet-like) software device that shows up as if it were a real NIC.
2. File layout – who does what?
Part
Code location
What it does
Imports / use lines
Top of file
Pull in nix crate helpers (open, ioctl), std::process::Command, etc.
const block
Top of file
Magic numbers Linux needs to understand which operation we want (TUNSETIFF, flags, size).
struct Ifreq
C-compatible packet the kernel expects when we create the device.
create_tap_device()
Opens /dev/net/tun, fills in Ifreq, calls ioctl, prints TAP device created, returns a file-descriptor.
configure_tap_device()
Runs three ip commands (ip addr add …, ip link set … up, ip route add …).
main()
Calls the two helpers above, then closes the fd (cleanup).
3. Line-by-line guided tour
rust
use nix::fcntl::{open, OFlag}; // thin wrappers around the libc open()
use nix::ioctl_write_ptr; // macro builds a safe-ish ioctl wrapper
use nix::sys::stat::Mode; // permission bits placeholder
use nix::unistd::close; // close() wrapper
use std::ffi::CString; // C-style null-terminated strings
use std::os::unix::io::RawFd; // fd type alias
use std::process::Command; // to run /sbin/ip commands
Kernel “magic numbers”
rust
const TUNSETIFF: u64 = 0x400454ca; // "create a tun/tap device" ioctl id
const IFF_TAP: i16 = 0x0002; // choose TAP (Ethernet) not TUN (IP)
const IFF_NO_PI:i16 = 0x1000; // do not prepend extra 4-byte header
const IFNAMSIZ: usize = 16; // Linux fixed buffer for names like "eth0"
ifreq – exactly like in C
rust
#[repr(C)] // keep same field order / padding as C struct
struct Ifreq {
ifr_name: [u8; IFNAMSIZ], // "tap0\0……"
ifr_flags: i16, // bitmask (TAP | NO_PI)
}
create_tap_device()
rust
let tun_path = CString::new("/dev/net/tun")?; // build C string
let fd = open( // open char device
tun_path.as_c_str().as_ptr(),
OFlag::O_RDWR, // read+write
Mode::empty(), // (not used)
)?;
Now prepare the request:
rust
let mut ifr = Ifreq { ifr_name:[0;IFNAMSIZ], ifr_flags: IFF_TAP|IFF_NO_PI };
let tap_name = CString::new("tap0")?;
ifr.ifr_name[..tap_name.as_bytes().len()].copy_from_slice(tap_name.as_bytes());
• unsafe because ioctl is inherently FFI, Rust can’t guarantee safety.
• On success Linux returns with a live TAP interface and the open fd
(kernel pushes packets through that fd).
configure_tap_device()
Instead of ioctl gymnastics, we shell-out to the ip(8) utility:
ip link set tap0 up
ip route add 192.168.1.0/24 dev tap0
We read output.status.success(); if any command fails we bubble up an
error with the stderr text.
main()
rust
let tap_fd = create_tap_device()?; // step 1
configure_tap_device()?; // step 2-3
close(tap_fd)?; // step 4 (fd no longer needed)
Ok(())
4. FAQ & Practical notes
Q. Why do we open /dev/net/tun instead of /dev/tap0?
A. /dev/net/tun is a control device. We pass a request (via ioctl) that
says “please create a new TAP interface called tap0 and bind it to this
file descriptor”.
Q. Why is unsafe required?
A. The compiler cannot verify that our Ifreq layout or ioctl number are
correct. We promise (“unsafe”) that they are.
Q. Could we avoid running sudo ip … from Rust?
A. Yes—by calling more ioctls (SIOCSIFADDR, SIOCSIFFLAGS, etc.) or using
the netlink socket interface. The example stays short by re-using the
well-tested ip binary.
Q. What happens to packets?
A. Any frame the kernel wants to send through tap0 will appear as bytes
you can read() from fd. Likewise, bytes you write() to fd are
injected into the kernel as if they arrived on a real NIC.
TL;DR
Open /dev/net/tun.
Tell the kernel “create TAP named tap0, no packet-info” (ioctl).
Give tap0 an IP & route with simple shell commands.
Close the fd (or keep it to exchange Ethernet frames).
That’s the whole program, step by step, in Rust. 🦀