rawether documentation

How to send and receive raw Ethernet frames from Rust with rawether: filters, the receive ring, timestamps, batched and launch-time transmission, event loops, Linux and BSD backends, privileges and the threat model.

rawether is a Rust library for raw layer-2 Ethernet I/O. You open a Socket on a named interface and choose which frames reach it. You then receive whole frames, starting at the destination MAC address, and send frames you built yourself. rawether uses the kernel’s own mechanisms for this: AF_PACKET on Linux, BPF devices on macOS and FreeBSD.

rawether is pre-1.0: the API may change between minor versions. The API reference on docs.rs is the authority on signatures.

How it works#

  1. Configure. Config::new("eth0") describes the socket: a filter, multicast groups, the receive path, timestamping, send options.
  2. Open. Config::open needs CAP_NET_RAW on Linux and /dev/bpf* access on macOS and FreeBSD. The filter is in place before the socket is bound, so nothing unwanted is ever queued.
  3. Drop the privilege. drop_capabilities() sheds CAP_NET_RAW. The open socket keeps working.
  4. Receive and send. recv hands out a Packet borrowed from the ring or buffer; send, send_batch and send_at take frames as byte slices.

Core concepts#

All pages

StartGetting startedInstall rawether, give a binary the privilege to open raw sockets without running as root, and receive and send your first Ethernet frames on Linux, macOS or FreeBSD.StartHow it worksWhat rawether does when it opens a raw Ethernet socket and receives a frame: the open sequence that keeps unfiltered frames out, the TPACKET_V3 ring shared with the kernel, borrowed packets and the allocation-free steady state.GuidesFiltering framesChoose which Ethernet frames reach a rawether socket: EtherType filters that see through 802.1Q and 802.1ad VLAN tags, multicast groups, promiscuous mode, your own classic-BPF programs and how to test them without a kernel.GuidesReceiving framesReceive raw Ethernet frames with rawether: blocking, timed and non-blocking receive, the zero-copy TPACKET_V3 ring and how to size it, the plain recvmsg path, truncated frames, VLAN tag restoration and counting drops.GuidesTimestampsReceive and transmit timestamps in rawether: the kernel's or the NIC's clock, labelled per frame, transmit timestamps matched to their frame by id, the PTP hardware clock behind an interface, and a NIC configuration that is never lowered.GuidesSending framesSend raw Ethernet frames with rawether: frame layout and padding, batched transmission with sendmmsg, traffic priority and VLAN PCP with SO_PRIORITY, and bypassing the queueing discipline on Linux.GuidesLaunch-time transmission (TSN)Send Ethernet frames at a precise time with rawether on Linux: SO_TXTIME, the etf and taprio qdiscs for Time-Sensitive Networking, the TAI clock, deadline mode, and counting frames the kernel dropped for missing their launch time.GuidesEvent loops and asyncUse a rawether socket in an event loop: AsFd and AsRawFd, non-blocking receive, poll, mio and a tokio AsyncFd example, without an async runtime dependency in the crate.PlatformsLinuxrawether on Linux: AF_PACKET sockets, the TPACKET_V3 receive ring and recvmsg fallback, kernel version requirements, CAP_NET_RAW and containers, and the kernel facilities each feature uses.PlatformsmacOS and FreeBSDrawether on macOS and FreeBSD: BPF devices, access to /dev/bpf, in-kernel filters, multicast membership, software timestamps, and what differs from Linux.SecurityThreat modelWhat rawether defends against when it opens raw Ethernet sockets: the ring shared with the kernel, hostile frames, the CAP_NET_RAW privilege, descriptor leaks and silent drops. What it trusts, what is out of scope, and the test that guards each class.SecurityTestingHow rawether is tested: unit tests for the ring parser, VLAN re-insertion and BPF interpreter, a kernel suite on a real veth pair on both receive paths, a counting allocator for the steady state, and CI on Linux, macOS and Windows. How to run it yourself.HelpPerformanceReceive throughput and latency of rawether's block ring, frame ring and plain paths against libpcap (the pcap crate) and pnet_datalink, measured over a veth pair, with the method and the command to reproduce it.HelpFAQCommon questions about rawether: how it compares with pnet and pcap, running without root, Windows, async, packet parsing, VLANs, PTP, dropped frames and reporting a vulnerability.