Rust crate · Linux, macOS, FreeBSD · pre-1.0

Raw Ethernet frames in Rust, filtered and timestamped by the kernel.

rawether sends and receives whole layer-2 frames on a named interface. On Linux it uses AF_PACKET with a zero-copy TPACKET_V3 ring, in-kernel BPF filters, hardware timestamps and SO_TXTIME launch times. On macOS and FreeBSD it uses BPF devices. One small, safe API, and one dependency: libc.

cargo add rawether
use std::time::Duration;
use rawether::EtherType;

// LLDP and PTP frames, VLAN-tagged or not.

let mut sock = rawether::Config::new("eth0")
    .ethertypes([EtherType::LLDP, EtherType::PTP])
    .open()?;

// CAP_NET_RAW is no longer needed.
rawether::drop_capabilities()?;

// Receive until a second passes without a frame.
while let Some(p) = sock.recv(Some(Duration::from_secs(1)))? {
    let Some(f) = p.frame() else { continue };
    let tags = f.vlan_tags().len();
    println!("{} sent {}, {tags} tag(s), at {:?}",
        f.source(), f.ethertype(), p.timestamp());
}

The filter runs in the kernel; p borrows the ring until the next recv, and p.frame() reads the header without ever panicking.

For everything below IP

Protocols that have no port number and live directly on Ethernet: time synchronisation, discovery, redundancy, industrial and automotive buses, and your own test traffic.

  • PTPIEEE 1588 · 0x88F7
  • LLDPIEEE 802.1AB · 0x88CC
  • TSN802.1Qbv · etf · taprio
  • PRP / HSRIEC 62439-3
  • PROFINETRT · 0x8892
  • EtherCAT0x88A4
  • IEC 61850GOOSE · Sampled Values
  • Custom0x88B5 · 0x88B6

How a frame reaches you

rawether hands the work to the kernel. Unwanted frames never leave it, wanted ones are read in place from memory shared with the kernel, and every frame records whose clock stamped it.

  1. 1Wire and NICThe NIC stamps the frame on its own clock, if it has hardware timestamping.
  2. 2Classic BPF filterYour EtherTypes, tagged or not, are checked in the kernel. Anything else is discarded there.
  3. 3TPACKET_V3 ringThe frame is written into memory shared with your process, with the VLAN tag the driver stripped put back.
  4. 4Packet<'_>A borrowed view into the ring with its lengths and timestamp. The compiler ends the borrow before the memory goes back to the kernel.

On Linux, as shown. When the kernel refuses the ring, the default mode uses recvmsg and says which path it is on. On macOS and FreeBSD the filter runs on the BPF device and frames arrive in a read buffer. See How it works.

Why rawether

The kernel features that make raw Ethernet fast and precise, behind an API that is hard to misuse.

Filtered in the kernel

An EtherType set, untagged, 802.1Q, 802.1ad or double-tagged, compiles to classic BPF. The filter is attached before the socket is bound, so no unwanted frame is ever queued. You can also bring your own program and test it without a kernel.

Filtering frames

Zero-copy receive

A TPACKET_V3 ring hands out frames borrowed from shared memory. The borrow ends at the next receive, enforced by the compiler, and the steady-state loop makes no allocations.

Receiving frames

Timestamps that say whose they are

Received and transmitted frames carry their timestamp and which clock made it, the NIC's or the kernel's; transmit timestamps come back matched to their frame by id, with the PTP hardware clock behind the interface. Safe next to ptp4l.

Timestamps

VLAN tags as on the wire

Many drivers strip the 802.1Q tag into metadata. rawether puts it back in place, in head-room the ring reserves, so your parser sees the frame as it was sent.

VLAN tags

Frames without index arithmetic

p.frame() gives you the addresses, up to two VLAN tags, the EtherType and the payload, and returns nothing instead of panicking on a short frame. FrameBuilder writes frames into a reused buffer, padded to the minimum.

Reading the header

Batched and timed send

sendmmsg sends up to 64 frames per system call. SO_TXTIME launch times feed etf and taprio, and missed deadlines are counted, not lost silently.

Launch-time transmission

Drops are counted

stats() keeps a running total of the kernel's received and dropped counters, so a full ring shows up as a number instead of a silent gap.

Counting drops

Any event loop

AsFd and AsRawFd, non-blocking mode, timed and blocking receive. Plugs into poll, mio or tokio without an async dependency in the crate.

Event loops and async

Least privilege

Grant CAP_NET_RAW to the binary, not root. drop_capabilities() sheds every capability for the whole process once the sockets are open, or refuses if a thread would keep one. Every descriptor is close-on-exec.

Threat model

Tested on a real kernel

Kernel behaviour is checked in CI on Linux, macOS and FreeBSD, from drops under load to links going down; the parsers run under Miri and a fuzzer. The block ring receives 2.8 M frames/s over veth, 1.8× libpcap; the frame ring answers in 15 µs.

Performance

Platforms

The same API on every Unix rawether supports. A request a platform cannot honour, such as a launch time on macOS, is refused with an error instead of being quietly dropped.

What rawether uses on each platform
FeatureLinux (AF_PACKET)macOS, FreeBSD (BPF)
EtherType and custom BPF filtersIn the kernelIn the kernel
ReceiveTPACKET_V3 ring, recvmsg fallbackBPF read buffer
TimestampsHardware or software, labelledSoftware
Multicast, promiscuousMembership, all-multicast, promiscuousMembership, promiscuous (covers all-multicast)
Batched sendsendmmsgOne write per frame
Launch time, priority, qdisc bypassSO_TXTIME, SO_PRIORITY, PACKET_QDISC_BYPASSLaunch time refused; the others ignored
Drop countersPACKET_STATISTICSBIOCGSTATS, frames read
PrivilegeCAP_NET_RAW, dropped after openingRead/write access to /dev/bpf*

Linux 3.2 or later for the ring and 4.19 or later for SO_TXTIME. Rust 1.88 or later. Other platforms compile, and Socket::open returns ErrorKind::Unsupported. Details: Linux, macOS and FreeBSD.

What it is not

rawether moves frames. It does not parse protocols, authenticate senders, or capture to pcap files. Layer 2 has no sender identity, so a frame can claim any source address. Use the protocol's own security (MACsec, IEC 62351-6, PTP authentication TLVs) where that matters.

It does not support Windows. If you need Windows, or a packet-parsing toolkit, the FAQ compares the alternatives. The threat model lists what rawether defends against and the test that guards each claim.

Status: pre-1.0. The API may change between minor versions.

Links