afpacket only.Expand description
AF_PACKET sockets: attach to an existing network interface.
This is the plainest way to put a real NIC into a topology. Unlike the
tuntap feature it does not create a new interface — it binds to one that
already exists — and unlike the afxdp feature it needs no eBPF, no driver
support and no special interface configuration. It is the slowest of the
three and the one most likely to just work.
(Those modules are named without links here because either may be compiled out while this one is not.)
use pktkit::afpacket::{Config, Socket};
use pktkit::{Frame, L2Device};
use std::sync::Arc;
let dev = Socket::open(Config {
interface: "eth0".into(),
promiscuous: true,
..Default::default()
})?;
dev.set_handler(Arc::new(|f: &Frame| {
println!("{:?} -> {:?}", f.src_mac(), f.dst_mac());
Ok(())
}));§Privileges
Opening an AF_PACKET socket needs CAP_NET_RAW. Without it, Socket::open
fails with PermissionDenied.
§What you will see
A bound socket receives every frame the interface accepts, including traffic
for the host itself, and — with Config::promiscuous set — traffic
addressed to other stations. Frames the host sends are also delivered back
unless Config::inbound_only is set. Frames written with
send go out the interface as-is, so the Ethernet header
is yours to fill in.
§Platform support
Linux only. On other platforms the type is still present so cross-platform
code compiles, but Socket::open returns ErrorKind::Unsupported.