rawether
🔌 Raw layer-2 Ethernet I/O for Rust. Send and receive whole Ethernet frames on a named
interface, with the kernel features that make it fast and precise: a TPACKET_V3 receive ring,
hardware and software timestamps, in-kernel classic-BPF filters, batched and launch-time
(SO_TXTIME) transmission on Linux; BPF devices on macOS and FreeBSD. One small, safe API;
one dependency (libc).
For anything that lives below IP: PTP (0x88F7), LLDP (0x88CC), PRP/HSR, PROFINET, EtherCAT, TSN streams, industrial process buses, custom test traffic.
use Duration;
let mut sock = new
.ethertypes // LLDP and PTP, VLAN-tagged or not
.open?;
drop_capabilities?; // CAP_NET_RAW is no longer needed
// Receive until a second passes without a frame.
while let Some = sock.recv?
Sending is the other half: describe the header once and write frames into a buffer you reuse.
use ;
let mut buf = Vecwith_capacity;
new
.write; // header, payload, padded to 60 octets
sock.send?;
📖 Documentation · 📚 API reference · 🔐 Threat model · 📐 Design records · 📝 Changelog · 🚨 Report a vulnerability
🚧 Pre-1.0. The API may change between minor versions.
✨ Features
- 🎯 Filtered in the kernel. A set of EtherTypes — untagged, 802.1Q, 802.1ad or double-tagged
— compiles to classic BPF and is attached before the socket is bound, so no unwanted frame is
ever queued. Or bring your own program;
bpf::Program::runinterprets it for tests. - 🌀 Zero-copy receive. On Linux a TPACKET_V3 ring hands out frames borrowed from shared
memory; a frame's borrow ends at the next receive, enforced by the compiler. Falls back to
recvmsgwhen the kernel refuses the ring, and says which path it is on. - ⏱️ Timestamps that say whose they are. Hardware, kernel software, or rawether's own read
for the few frames the kernel did not stamp, each labelled. The NIC's device-wide stamping
configuration is read first and never lowered, so it is safe next to
ptp4l. - 🏷️ VLAN tags as on the wire. A tag the driver stripped into metadata is put back in place, using head-room the ring reserves.
- 🧩 Frames without index arithmetic.
p.frame()names the addresses, up to two VLAN tags (PCP, DEI, VID), the EtherType and the payload, and never panics on a short frame.FrameBuilderwrites frames into a reused buffer, padded to the minimum;replyanswers one. - 🚀 Batched and timed send.
sendmmsgbatches;SO_TXTIMElaunch times foretf/taprio;SO_PRIORITY; optional qdisc bypass. - 📊 Drops are counted.
stats()reports received and dropped frames, with one meaning on every platform;tx_errors()says why launch-time frames were dropped. - 🔁 Any event loop.
AsFd/AsRawFd, non-blocking mode, timed and blocking receive. - 🛡️ Least privilege. Every descriptor is close-on-exec;
drop_capabilities()sheds every capability for the whole process once the sockets are open, or refuses if another thread would keep one. - 🧱 No allocation in the steady state, measured by a counting allocator.
🖥️ Platforms
| Linux (AF_PACKET) | macOS, FreeBSD (BPF) | |
|---|---|---|
| EtherType filter, tagged or not; custom classic BPF | ✓ in kernel | ✓ in kernel |
| Multicast membership · all-multicast · promiscuous | ✓ · ✓ · ✓ | ✓ · via promiscuous · ✓ |
| Receive | TPACKET_V3 ring, recvmsg fallback |
read buffer |
| Timestamps | hardware or software, labelled | software |
| Stripped VLAN tags put back | ✓ | not needed |
| Batched send | sendmmsg |
loop |
Launch time (SO_TXTIME), SO_PRIORITY, qdisc bypass |
✓ | — |
| Drop counters | PACKET_STATISTICS |
BIOCGSTATS |
| Privilege | CAP_NET_RAW (drop_capabilities() after opening) |
access to /dev/bpf* |
Other platforms compile; Socket::open returns ErrorKind::Unsupported.
Tested releases — every lane runs on each push:
| Lane | What runs |
|---|---|
ubuntu-24.04 |
unit tests; the veth suite on the ring and the plain path; the capability drop; clippy for Linux, macOS, FreeBSD, Windows and 32-bit; MSRV, docs, cargo-deny |
macos-15 |
unit tests, including opening a BPF device |
| FreeBSD 14 (VM) | unit tests, including both BPF record layouts |
| Miri, fuzz | the pure parsers under Miri; five fuzz targets, 60 s each |
windows-2025 |
build; open returns Unsupported |
Linux ≥ 3.2 for the ring, ≥ 4.19 for SO_TXTIME. Rust ≥ 1.88.
🧪 Examples
On Linux, grant the capability to the binary instead of running it as root:
sudo setcap cap_net_raw+ep target/debug/examples/sniff.
🔧 Develop
The website's sources are in site/; it is built with Zola
and deployed to GitHub Pages from main.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.