JFLib

netpkt/eth.rs#

Source: lib/netpkt/eth.rs

eth -- Ethernet II frames, IEEE 802.1Q tags and 802.2 LLC

  EthView::parse(frame)       dst, src, ethertype (or 802.3 length), payload
  EthHdr { dst, src, ethertype }  write / push_onto(&mut Pkt)
  VlanTag { pcp, dei, vid }   tag_of(frame), tagged(frame, tag), untagged(frame)
  L2::parse(frame)            dst, src, the 802.1Q tag if any, the inner ethertype and where
                              the payload starts
  Llc                         802.2 LLC header of 802.3 frames (STP uses DSAP/SSAP 0x42)
  pad(frame)                  pads to the 60-byte minimum (without FCS)

Frames here never carry the FCS; checksum::crc32 computes it when someone wants to see it.
ETH_HDR: usize = 14
VLAN_TAG: usize = 4
/// the shortest frame without FCS
ETH_MIN: usize = 60
/// the largest payload of a standard frame
ETH_MTU: usize = 1500
ET_IPV4: u16 = 0x0800
ET_ARP: u16 = 0x0806
ET_VLAN: u16 = 0x8100
ET_IPV6: u16 = 0x86DD
ET_QINQ: u16 = 0x88A8
ET_LLDP: u16 = 0x88CC
/// IEEE 802.1X (EAPOL: the 4-way handshake of WPA2 and WPA3)
ET_EAPOL: u16 = 0x888E
  • fn ethertype_name(t: u16) -> Option<&'static str> a name for an ethertype ("IPv4", "ARP", ...), None for unknown types

  • struct EthView<'a> a received Ethernet frame (at least 14 bytes)

impl<'a> EthView<'a>

  • fn parse(b: &'a [u8]) -> Option<EthView<'a>>

  • fn dst(&self) -> Mac

  • fn src(&self) -> Mac

  • fn ethertype(&self) -> u16 the type field (an 802.3 length when <= 1500)

  • fn is_llc(&self) -> bool an IEEE 802.3 frame with a length field and an LLC header

  • fn payload(&self) -> &'a [u8]

  • fn bytes(&self) -> &'a [u8]

  • struct EthHdr the Ethernet II header to write

    • dst: Mac
    • src: Mac
    • ethertype: u16

impl EthHdr

  • const LEN: usize = ETH_HDR

  • fn read(v: &EthView) -> EthHdr

  • fn write(&self, out: &mut [u8])

  • fn push_onto(&self, p: &mut Pkt)

  • fn frame(&self, payload: &[u8]) -> Vec<u8> header + payload as a new frame

  • fn pad(frame: &mut Vec<u8>) pads a frame with zeros to the 60-byte minimum

  • struct VlanTag an IEEE 802.1Q tag: priority, drop eligible, VLAN id (1..4094)

    • pcp: u8
    • dei: bool
    • vid: u16

impl VlanTag

  • fn new(vid: u16) -> VlanTag

  • fn tci(&self) -> u16

  • fn from_tci(tci: u16) -> VlanTag

  • fn tag_of(frame: &[u8]) -> Option<VlanTag> the outer 802.1Q tag of a frame (TPID 0x8100), None for untagged frames

  • fn tagged(frame: &[u8], tag: VlanTag) -> Vec<u8> the frame with a tag inserted after the source MAC (an existing tag stays inside)

  • fn untagged(frame: &[u8]) -> Vec<u8> the frame without its outer tag (unchanged copy when untagged)

  • struct L2 the layer-2 view of a frame: addresses, an optional 802.1Q tag, the inner type

    • dst: Mac
    • src: Mac
    • tag: Option<VlanTag>
    • ethertype: u16 -- the ethertype after the tag (an 802.3 length when <= 1500)
    • off: usize -- offset of the payload in the frame

impl L2

  • fn parse(frame: &[u8]) -> Option<L2>

  • fn payload<'a>(&self, frame: &'a [u8]) -> &'a [u8]

  • struct Llc the IEEE 802.2 LLC header of an 802.3 frame (unnumbered information frames only)

    • dsap: u8
    • ssap: u8
    • control: u8
/// the SAP of the spanning tree protocols
SAP_STP: u8 = 0x42

impl Llc

  • const LEN: usize = 3
  • const STP: Llc = Llc { dsap: SAP_STP, ssap: SAP_STP, control: 0x03 }
  • fn parse(b: &[u8]) -> Option<Llc>
  • fn write(&self, out: &mut [u8])

Source: docs/API.md in the JFLib repository.