use nom::number; use nom::IResult; use std::convert::TryFrom; /****************************************************************************** * Struct ******************************************************************************/ #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct MacAddress(pub [u8; 6]); #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum EtherType { LANMIN, LANMAX, IPv4, ARP, WOL, TRILL, DECnet, RARP, AppleTalk, AARP, VLAN, IPX, Qnet, IPv6, FlowControl, CobraNet, MPLSuni, MPLSmulti, PPPoEdiscovery, PPPoEsession, HomePlug, EAPOL, PROFINET, HyperSCSI, ATAOE, EtherCAT, QinQ, Powerlink, GOOSE, GSE, LLDP, SERCOS, HomePlugAV, MRP, MACsec, PBB, PTP, PRP, CFM, FCoE, FCoEi, RoCE, TTE, HSR, CTP, VLANdouble, Other(u16), } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct EthernetFrame { pub src_mac: MacAddress, pub dst_mac: MacAddress, pub ether_type: EtherType, } /****************************************************************************** * Parse ******************************************************************************/ impl From for EtherType { fn from(raw: u16) -> Self { match raw { 0x002E => Self::LANMIN, // 802.3 Min data length 0x05DC => Self::LANMAX, // 802.3 Max data length 0x0800 => Self::IPv4, // Internet Protocol version 4 (IPv4) [RFC7042] 0x0806 => Self::ARP, // Address Resolution Protocol (ARP) [RFC7042] 0x0842 => Self::WOL, // Wake on LAN 0x22F3 => Self::TRILL, // IETF TRILL Protocol [IEEE] 0x6003 => Self::DECnet, // DECnet Phase IV 0x8035 => Self::RARP, // Reverse Address Resolution Protocol (RARP) [RFC903] 0x809B => Self::AppleTalk, // AppleTalk - EtherTalk [Apple] 0x80F3 => Self::AARP, // AppleTalk Address Resolution Protocol (AARP) [Apple] 0x8100 => Self::VLAN, // VLAN-tagged frame (IEEE 802.1Q) and Shortest Path Bridging IEEE 802.1aq[5] 0x8137 => Self::IPX, // IPX [Xerox] 0x8204 => Self::Qnet, // QNX Qnet [QNX Software Systems] 0x86DD => Self::IPv6, // Internet Protocol Version 6 (IPv6) [RFC7042] 0x8808 => Self::FlowControl, // Ethernet Flow Control [IEEE 802.3x] 0x8819 => Self::CobraNet, // CobraNet [CobraNet] 0x8847 => Self::MPLSuni, // MPLS Unicast [RFC 3032] 0x8848 => Self::MPLSmulti, // MPLS Multicast [RFC 5332] 0x8863 => Self::PPPoEdiscovery, // PPPOE Discovery Stage [RFC 2516] 0x8864 => Self::PPPoEsession, // PPPoE Session Stage [RFC 2516] 0x887B => Self::HomePlug, // HomePlug 1.0 MME 0x888E => Self::EAPOL, // EAP over LAN (IEEE 802.1X) 0x8892 => Self::PROFINET, // PROFINET Protocol 0x889A => Self::HyperSCSI, // HyperSCSI (SCSI over Ethernet) 0x88A2 => Self::ATAOE, // ATA over Ethernet 0x88A4 => Self::EtherCAT, // EtherCAT Protocol 0x88A8 => Self::QinQ, // Provider Bridging (IEEE 802.1ad) & Shortest Path Bridging IEEE 802.1aq[5] 0x88AB => Self::Powerlink, // Ethernet Powerlink[citation needed] 0x88B8 => Self::GOOSE, // GOOSE (Generic Object Oriented Substation event) 0x88B9 => Self::GSE, // GSE (Generic Substation Events) Management Services 0x88CC => Self::LLDP, // Link Layer Discovery Protocol (LLDP) [IEEE 802.1AB] 0x88CD => Self::SERCOS, // SERCOS III 0x88E1 => Self::HomePlugAV, // HomePlug AV MME[citation needed] 0x88E3 => Self::MRP, // Media Redundancy Protocol (IEC62439-2) 0x88E5 => Self::MACsec, // MAC security (IEEE 802.1AE) 0x88E7 => Self::PBB, // Provider Backbone Bridges (PBB) (IEEE 802.1ah) 0x88F7 => Self::PTP, // Precision Time Protocol (PTP) over Ethernet [IEEE 1588] 0x88FB => Self::PRP, // Parallel Redundancy Protocol (PRP) 0x8902 => Self::CFM, // IEEE 802.1ag Connectivity Fault Management (CFM) Protocol / ITU-T Recommendation Y.1731 (OAM) 0x8906 => Self::FCoE, // Fibre Channel over Ethernet (FCoE) 0x8914 => Self::FCoEi, // FCoE Initialization Protocol 0x8915 => Self::RoCE, // RDMA over Converged Ethernet (RoCE) 0x891D => Self::TTE, // TTEthernet Protocol Control Frame (TTE) 0x892F => Self::HSR, // High-availability Seamless Redundancy (HSR) 0x9000 => Self::CTP, // Ethernet Configuration Testing Protocol[6] 0x9100 => Self::VLANdouble, // VLAN-tagged (IEEE 802.1Q) frame with double tagging other => Self::Other(other), } } } fn parse_mac_address(input: &[u8]) -> IResult<&[u8], MacAddress> { let (input, mac_address) = nom::bytes::streaming::take(6u8)(input)?; Ok((input, MacAddress(<[u8; 6]>::try_from(mac_address).unwrap()))) } fn parse_ether_type(input: &[u8]) -> IResult<&[u8], EtherType> { let (input, ether_type) = number::streaming::be_u16(input)?; Ok((input, ether_type.into())) } pub fn parse_ethernet(input: &[u8]) -> IResult<&[u8], EthernetFrame> { let (input, dst_mac) = parse_mac_address(input)?; let (input, src_mac) = parse_mac_address(input)?; let (input, ether_type) = parse_ether_type(input)?; Ok(( input, EthernetFrame { src_mac, dst_mac, ether_type, }, )) } /****************************************************************************** * TEST ******************************************************************************/ #[cfg(test)] mod tests { use super::parse_ethernet; use super::{EtherType, EthernetFrame, MacAddress}; const LAST_SLICE: &'static [u8] = &[0xff]; #[test] fn parse_ethernet_works() { /* * Ethernet II, Src: Apple_0a:c5:ea (3c:a6:f6:0a:c5:ea), Dst: Charge-A_08:02:be (4c:bc:98:08:02:be) * Destination: Charge-A_08:02:be (4c:bc:98:08:02:be) * Address: Charge-A_08:02:be (4c:bc:98:08:02:be) * .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default) * .... ...0 .... .... .... .... = IG bit: Individual address (unicast) * Source: Apple_0a:c5:ea (3c:a6:f6:0a:c5:ea) * Address: Apple_0a:c5:ea (3c:a6:f6:0a:c5:ea) * .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default) * .... ...0 .... .... .... .... = IG bit: Individual address (unicast) * Type: IPv4 (0x0800) */ let bytes = [ 0x4c, 0xbc, 0x98, 0x08, 0x02, 0xbe, /* Destination Address */ 0x3c, 0xa6, 0xf6, 0x0a, 0xc5, 0xea, /* Source Address */ 0x08, 0x00, /* Type */ 0xff, /* Payload */ ]; let expectation = EthernetFrame { src_mac: MacAddress([0x3c, 0xa6, 0xf6, 0x0a, 0xc5, 0xea]), dst_mac: MacAddress([0x4c, 0xbc, 0x98, 0x08, 0x02, 0xbe]), ether_type: EtherType::IPv4, }; assert_eq!(parse_ethernet(&bytes), Ok((LAST_SLICE, expectation))); } }