Data link
The data link layer is the first one parse reads, and the only one whose format the bytes do not announce. This page explains how the crate selects the format; each format then has its own page.
The LINKTYPE decides: LinkType and LinkLayer
A capture is not always Ethernet. A capture on the Linux any interface is Linux cooked (SLL), a VPN capture is often RAW IP, an industrial capture can be 802.3br. The bytes of these formats look nothing like an Ethernet header, and nothing in them says which format they are. Only the capture file knows, through its LINKTYPE.
That is why parse takes a LinkType and never guesses. LinkType(pub u32) uses the canonical LINKTYPE_* values stored in PCAP/PCAPNG files, and stays open: an unknown value is preserved, not collapsed into an Unknown variant. parse never reads a capture file, so mapping the capture source to this namespace is the caller's job: a DLT_* value from a live capture must be normalized when it differs from the LINKTYPE_* value, and a PCAPNG reader passes the LINKTYPE of the interface each packet references.
#![allow(unused)] fn main() { /// Single source of truth for the link types backed by a decoder. #[inline(always)] const fn decoder_for(link_type: LinkType) -> Option<DecoderKind> { match link_type { LinkType::ETHERNET => Some(DecoderKind::Ethernet), // BSD loopback: four bytes of address family, then the IP packet. LinkType::NULL => Some(DecoderKind::NullLoopback), // RAW, IPV4 and IPV6 share the same shape: the bytes start directly // at the IP header. RawIpDecoder reads the version from the first // nibble, which covers all three without a dedicated decoder. LinkType::RAW => Some(DecoderKind::RawIp(LinkType::RAW)), LinkType::IPV4 => Some(DecoderKind::RawIp(LinkType::IPV4)), LinkType::IPV6 => Some(DecoderKind::RawIp(LinkType::IPV6)), LinkType::LINUX_SLL => Some(DecoderKind::LinuxSll), LinkType::LINUX_SLL2 => Some(DecoderKind::LinuxSll2), LinkType::IEEE802_3BR => Some(DecoderKind::Ieee8023br), _ => None, } } }
This function is the single source of truth: is_supported is decoder_for(..).is_some(), and parse returns ParseError::UnsupportedLinkType before reading a single byte when it is None. Two constants have no decoder: LinkType::IEEE802_11 (105) and LinkType::BLUETOOTH_HCI_H4_WITH_PHDR (201) are named, and refused like any unknown value.
Every decoder produces the same format-neutral output, consumed by the shared L3/L4/L7 pipeline:
#![allow(unused)] fn main() { #[non_exhaustive] pub struct LinkLayer<'a> { link_type: LinkType, // the LINKTYPE it was decoded as network_protocol: NetworkProtocol, // what comes next: Ipv4, Ipv6, Arp, Profinet, // Ptp, Goose, Lldp, or Other(u16) network_payload: &'a [u8], // the L3 bytes kind: LinkLayerKind<'a>, // the format-specific view } #[non_exhaustive] pub enum LinkLayerKind<'a> { Ethernet(DataLink<'a>), RawIp(RawIpLink<'a>), LinuxSll(LinuxSllLink<'a>), LinuxSll2(LinuxSll2Link<'a>), Ieee80211(Ieee80211Link<'a>), } }
The fields are private and every constructor is format-specific (LinkLayer::ethernet or From<DataLink>, LinkLayer::ieee80211, crate-internal ones for the other decoders), so link_type, network_protocol and kind cannot disagree. LinkLayerKind is #[non_exhaustive]: a new link format is a new variant, and a match on it needs a wildcard arm.
NetworkProtocol is deliberately independent from Ethernet: RAW IP announces Ipv4 or Ipv6 without fabricating an EtherType, while Ethernet and SLL map their protocol field to the same value. Seven values are named, the protocols the internet layer decodes; any other is Other(u16) and gives no internet layer. For a PPPoE session frame carrying IP, the Ethernet view maps the EtherType reached once the session is peeled: an IPv4 packet in a PPPoE session is announced Ipv4, like a bare one. The common accessors (link_type(), network_protocol(), network_payload()) never assume Ethernet; the format-specific views are explicit (kind() to match on, or as_ethernet(), as_raw_ip(), as_linux_sll(), as_linux_sll2() and as_ieee80211(), which each return an Option):
#![allow(unused)] fn main() { println!("LINKTYPE={}", flow.data_link.link_type()); println!("next={:?}", flow.data_link.network_protocol()); if let Some(ethernet) = flow.data_link.as_ethernet() { println!("{} -> {}", ethernet.source_mac, ethernet.destination_mac); } if let Some(sll) = flow.data_link.as_linux_sll() { println!("{} type={}", sll.hardware_type, sll.packet_type); } }
A RAW or SLL capture therefore cannot come out with MAC addresses it never had, which is what happened when every format went through DataLink.
The formats
Each format has its page:
- Ethernet (LINKTYPE 1), with its MAC addresses, EtherType and VLAN tags;
- PPPoE session (EtherType
0x8864), peeled inside an Ethernet frame like a VLAN tag since 11.6.0; - Linux cooked capture, SLL (113) and SLL2 (276);
- RAW IP (101, 228, 229);
- BSD loopback (NULL, 0);
- IEEE 802.3br (274);
- and STP, which lives in Ethernet frames but has no network layer.
IEEE 802.11 is modelled (Ieee80211Link) for the inner flows of a CAPWAP tunnel, not yet decodable as a top-level LINKTYPE.
One error path
Whatever the format, a link failure is reported through the same enum, LinkLayerError (#[non_exhaustive]), with sizes that count the whole packet:
#![allow(unused)] fn main() { Err(ParseError::InvalidLinkLayer(LinkLayerError::Truncated { link_type, required, actual, })) }
Truncated is the only variant Ethernet produces: its decoder maps DataLinkError::DataLinkTooShort onto it, required counting the VLAN tags already met. The 802.3br decoder adds its preamble, SMD and mCRC to both sizes and the NULL decoder its four header bytes, so a consumer accounting for link failures never needs to know where a decoder slices the packet. The other variants name what is wrong instead of a size: InvalidIpVersion, InvalidAddressFamily, InvalidPreamble, InvalidSmd and PreemptibleFragment. An unsupported LINKTYPE is not among them: it is ParseError::UnsupportedLinkType. Neither is a malformed PPPoE session: the frame is kept unpeeled, with EtherType 0x8864 and no layer 3, never refused (PPPoE session).