Skip to main content

Crate rawether

Crate rawether 

Source
Expand description

Raw layer-2 Ethernet I/O.

Open a Socket on a named interface, choose which frames reach it, and send and receive whole Ethernet frames — with the kernel features that make that fast and precise:

Linux (AF_PACKET)macOS, FreeBSD (BPF)
EtherType filter, tagged or not; custom classic BPF✓ in kernel✓ in kernel
Multicast membership, all-multicast, promiscuous✓✓ (all-multicast via promiscuous)
Receive ring (TPACKET_V3), zero-copy✓, recvmsg fallbackread buffer
Receive timestampshardware or software, labelledsoftware
Stripped VLAN tags put back✓not needed
Batched sendsendmmsgloop
Launch-time send (SO_TXTIME), SO_PRIORITY, qdisc bypass✓—
Drop counters✓✓
Pollable fd, non-blocking, timed receive✓✓

Other platforms compile, and Socket::open returns std::io::ErrorKind::Unsupported.

use std::time::Duration;

// LLDP and PTP frames, VLAN-tagged or not, with timestamps.
use rawether::EtherType;
let mut sock = rawether::Config::new("eth0").ethertypes([EtherType::LLDP, EtherType::PTP]).open()?;
while let Some(p) = sock.recv(Some(Duration::from_secs(1)))? {
    let Some(f) = p.frame() else { continue }; // shorter than a header: skip, never panic
    println!("{} sent {} at {:?}", f.source(), f.ethertype(), p.timestamp());
}

Receiving hands out a Packet that borrows the socket’s ring or buffer until the next receive, so a steady-state loop never allocates.

§Frames

Packet::frame (or Frame::new on any byte slice) reads the Ethernet header: addresses, up to two VLAN tags, the EtherType and the payload, without index arithmetic and without panicking on a short frame. FrameBuilder writes a frame into a buffer you reuse, padded to the Ethernet minimum, and Frame::reply answers a received one:

let me = sock.mac_address();
let mut buf = Vec::with_capacity(1518);
loop {
    let Some(p) = sock.recv(None)? else { continue };
    let Some(reply) = p.frame().and_then(|f| f.reply(me)) else { continue };
    reply.write(&mut buf, b"pong"); // the builder holds no borrow of the packet
    sock.send(&buf)?;
}

§Privileges

Opening a socket needs CAP_NET_RAW on Linux and read/write access to /dev/bpf* on macOS and FreeBSD. Grant the capability to the binary (setcap cap_net_raw+ep) rather than running as root, and call drop_capabilities once every socket is open: the sockets keep working, and the code that parses untrusted frames no longer holds the privilege.

§Security

The threat model states what rawether defends against (the ring shared with the kernel, hostile frame shapes, the privilege, descriptor leaks, silent drops), what it trusts, and the test that guards each claim. Report a vulnerability privately through GitHub’s private vulnerability reporting.

More guides, including launch-time transmission, hardware timestamps and event-loop integration, are on the rawether website.

Re-exports§

pub use frame::Frame;
pub use frame::FrameBuilder;
pub use frame::TagKind;
pub use frame::VlanTag;

Modules§

bpf
Classic BPF: the in-kernel filter language both backends run.
frame
Ethernet frames: read the header of a frame, and build one to send.
threat_model
Threat model

Structs§

Config
Everything Socket::open needs.
EtherType
An EtherType. Any 16-bit value works; the constants name the well-known ones.
FrameRingConfig
The geometry of a per-frame (TPACKET_V2) receive ring: one slot per frame.
MacAddr
A 48-bit Ethernet hardware address.
Packet
One received frame, borrowed from the socket until the next receive.
RingConfig
The geometry of a TPACKET_V3 receive ring.
Socket
An open raw socket on one interface.
Stats
Frame counters for a socket, accumulated since it was opened. They mean the same on every platform; neither counts frames lost before the socket (in the NIC or the kernel’s per-CPU backlog).
Timestamp
When a frame was received, and by whose clock.
TimestampInfo
What an interface can timestamp, and which PTP hardware clock stamps it.
TxErrors
Launch-time frames the kernel dropped, by reason, since the last Socket::tx_errors.
TxTime
Launch-time transmission (SO_TXTIME, Linux).
TxTimestamp
When a sent frame left, by its identifier (Socket::next_tx_id).

Enums§

Clock
A clock a launch time is expressed on.
Filter
Which frames reach the socket.
RxMode
How received frames reach user space.
RxPath
How frames reach user space.
TimestampSource
Who stamped a frame.
Timestamping
Receive timestamps.

Functions§

clock_now
Read clock — for computing launch times.
drop_capabilities
Give up the privilege that opening a socket needed, for the whole process, keeping every socket already open.
timestamp_info
What interface can timestamp, and which PTP hardware clock (/dev/ptp<index>) its hardware stamps come from. Needs no privilege. On macOS and FreeBSD, BPF stamps received frames in software and there is no hardware clock.