Ethernet

LINKTYPE 1, the format of most captures. The data link page explains how the LINKTYPE selects this decoder.

The frame

The Ethernet II frame: destination MAC, source MAC, optional VLAN tags, EtherType, payload

BytesFieldMeaning
0–5Destination MAC addressthe receiving interface, or a group of interfaces (multicast, broadcast)
6–11Source MAC addressthe sending interface
12–13EtherTypethe protocol of the payload, or a tag that precedes it
14–Payloadthe packet of the next layer (IPv4, ARP...)

MAC addresses

A MAC address is six bytes. The first three are the Organizationally Unique Identifier (OUI) of the manufacturer:

A MAC address: a 3-byte OUI, here Siemens, then the 3-byte NIC-specific part; bit 0 of the first byte is the I/G bit

#![allow(unused)]
fn main() {
pub struct MacAddress(pub [u8; 6]);

impl MacAddress {
    pub const fn is_broadcast(&self) -> bool;
    /// The I/G bit of the first byte: broadcast is multicast too.
    pub const fn is_multicast(&self) -> bool;
    pub const fn is_unicast(&self) -> bool;
    /// "ASUSTek:3c:4d:5e" when the OUI is known (the manufacturer replaces
    /// the first three bytes), "00:1a:2b:3c:4d:5e" otherwise.
    pub fn display_with_oui(&self) -> String;
    pub fn get_oui(&self) -> Oui;
}
}

The OUI table is embedded in the crate: no file to load, no network lookup. It is also short: a match on seven prefixes (ASUSTek, Intel, Sagemcom, three Siemens prefixes and PnMc, the PROFINET multicast prefix 01:0e:cf), not the IEEE registry. Any other prefix is Oui::Unknown, and display_with_oui() falls back to the plain address.

The EtherType

The EtherType is a two-byte big-endian value that names the protocol of the payload. Ethertype(pub u16) is a plain newtype: an unknown value is preserved, not refused, and static_name() returns None for it. The values the crate names include:

EtherType (Hex)Protocol
0x0800IPv4
0x86DDIPv6
0x0806ARP
0x8892Profinet
0x88F7PTP
0x88B8GOOSE
0x88CCLLDP
0x8100VLAN tag (802.1Q)
0x88A8 / 0x9100Service tag (802.1ad / legacy QinQ)
0x8864PPPoE session

Naming is not decoding. Only the seven values that NetworkProtocol maps reach a decoder of the internet layer: IPv4, IPv6, ARP and Profinet, PTP (0x88F7) since 11.5.0, GOOSE (0x88B8) and LLDP (0x88CC) since 11.6.0. MRP (0x88E3), the PPPoE discovery stage (0x8863) and the rest of the name table only get a readable name, and internet stays None. The TPIDs never survive as the parsed ethertype: the parser consumes the tags they announce, and, since 11.6.0, a PPPoE session that carries IP (below).

VLAN tags

When the EtherType is a TPID (0x8100, 0x88A8 or 0x9100), the next 4 bytes are a VLAN tag (TCI + the following EtherType), and that EtherType may itself be a TPID: 802.1ad puts an S-tag in front of the C-tag, and some equipment stacks two 0x8100. The parser consumes the whole stack, so that ethertype and payload always describe the real layer 3:

#![allow(unused)]
fn main() {
#[non_exhaustive]
pub struct DataLink<'a> {
    pub destination_mac: MacAddress,
    pub source_mac: MacAddress,
    /// The innermost tag (the customer VLAN), if any.
    pub vlan: Option<VlanTag>,
    /// The whole stack, outermost first (S-tag then C-tag).
    pub vlan_stack: VlanStack<'a>,
    /// The PPPoE session the frame belongs to, if any (11.6.0).
    pub pppoe: Option<PppoeSession>,
    /// The real layer-3 EtherType, after the tags and, when PPP carries IP,
    /// the PPPoE session.
    pub ethertype: Ethertype,
    pub payload: &'a [u8],
}

pub struct VlanTag {
    pub id: u16,      // VLAN identifier (12 bits)
    pub pcp: u8,      // priority code point (3 bits)
    pub dei: bool,    // drop eligible indicator
    pub inner_ethertype: Ethertype,
}
}

vlan_stack.outer() gives the S-VLAN, vlan_stack.inner() the same tag as vlan, and vlan_stack.iter() walks the stack from outer to inner. VlanStack is a zero-copy view over the tag bytes of the frame: no allocation, whatever the depth (#82). It takes part in equality and hashing, so two frames that differ only by their S-tag are two flows, and it is serialized only when at least two tags are stacked, so the JSON of untagged and single-tagged frames did not change when it appeared in 11.0.0.

The Display of a VlanTag prints its ID and PCP in decimal since 11.5.0 (#134): ID: 100, PCP: 0, DEI: false, where VLAN 100 used to print as ID: 64, hexadecimal without a prefix. The Display of DataLink carries the same text.

The untagged case is the hot path and stays a straight line; the stack is unrolled in a separate #[inline(never)] function, because unrolling it inline cost about 13 ns on every frame, tagged or not (measured on the reference packet).

PPPoE sessions

When the EtherType, after the tags if there are any, is 0x8864, the frame belongs to a PPPoE session (RFC 2516), the link of a fibre or xDSL subscriber to the provider. Since 11.6.0 the session is peeled like a tag when PPP carries IPv4 (0x0021) or IPv6 (0x0057) (#172): ethertype and payload then describe that packet, bounded by the PPPoE length, and pppoe keeps the session, a PppoeSession with its identifier, its length and the PPP protocol. With a PPP control protocol (LCP, IPCP...), ethertype stays 0x8864 and payload is the PPP packet. The session identifier and the PPP protocol take part in equality and hashing, the PPPoE length does not, and the session is not serialized yet. The PPPoE session page has the details and the oracle.

Validations

The first bytes of an Ethernet frame go through DataLink::try_from: Ok(DataLink) or Err(DataLinkError)

BytesFieldCheckError
0–13Two MAC addresses and the EtherTypethe frame holds at least 14 bytesDataLinkError::DataLinkTooShort { required, actual }
14–VLAN stackchecked again at each tag consumed: 14 + 4 × tags bytesDataLinkTooShort
0–5, 6–11MAC addressexactly 6 bytesMacParseError::InvalidLength

A frame cut in the middle of the VLAN stack is reported as too short, never read out of bounds. The MAC check cannot fail from DataLink::try_from, where the frame length is already proven; it protects a direct MacAddress::try_from.

These are the errors of DataLink::try_from called directly. Through parse, the Ethernet decoder converts them into LinkLayerError::Truncated (see One error path): since 11.0.0, DataLinkError is no longer part of ParseError.

The EtherType is not validated: an unknown value is a valid frame carrying a protocol the crate does not decode, and the internet layer will simply be None. A PPPoE session header that is not well formed is no error either: the frame keeps EtherType 0x8864, pppoe is None, and there is no layer 3.

#![allow(unused)]
fn main() {
/// Ethernet II header: two MACs and an EtherType.
const DATALINK_HEADER_LEN: usize = 14;

impl<'a> TryFrom<&'a [u8]> for DataLink<'a> {
    type Error = DataLinkError;

    fn try_from(packets: &'a [u8]) -> Result<Self, Self::Error> {
        validate_data_link_length(packets)?;

        let destination_mac = MacAddress::try_from(&packets[0..6])?;
        let source_mac = MacAddress::try_from(&packets[6..12])?;

        let raw_ethertype = u16::from_be_bytes([packets[12], packets[13]]);
        if VlanTag::is_tpid(raw_ethertype) {
            return Self::parse_tagged(packets, destination_mac, source_mac, raw_ethertype);
        }
        if raw_ethertype == PPPOE_SESSION_ETHERTYPE {
            return Ok(Self::parse_pppoe(
                destination_mac,
                source_mac,
                None,
                VlanStack::default(),
                &packets[DATALINK_HEADER_LEN..],
            ));
        }

        Ok(DataLink {
            destination_mac,
            source_mac,
            vlan: None,
            vlan_stack: VlanStack::default(),
            pppoe: None,
            ethertype: Ethertype::from(raw_ethertype),
            payload: &packets[DATALINK_HEADER_LEN..],
        })
    }
}
}

Called directly, DataLink::try_from assumes Ethernet: it checks lengths, never that the bytes are Ethernet, so a Linux SLL frame comes back Ok with fabricated MAC addresses. It is kept for compatibility and announced for removal in a future major release. Inside the crate it is only called where something declares Ethernet: the LINKTYPE (Ethernet, 802.3br express mPackets) or a tunnel header (VXLAN, and GRE or Geneve announcing 0x6558).

The parsed frame

The frame of the book's SYN, bytes 0 to 5 into destination_mac, 6 to 11 into source_mac, 12 and 13 into ethertype, the rest into payload