From f2f0b91b513627135c81af2489c30561d3554f8d Mon Sep 17 00:00:00 2001 From: Jake Shadle Date: Tue, 4 Mar 2025 22:34:12 +0100 Subject: [PATCH 1/2] Change enums to module scoped constants --- crates/tests/benches/csum.rs | 4 +- src/packet/csum.rs | 6 +- src/packet/net_types.rs | 331 +++-------------------------------- 3 files changed, 30 insertions(+), 311 deletions(-) diff --git a/crates/tests/benches/csum.rs b/crates/tests/benches/csum.rs index 3b0d629..3bce901 100644 --- a/crates/tests/benches/csum.rs +++ b/crates/tests/benches/csum.rs @@ -90,7 +90,7 @@ fn csum_ic(packet: &mut xdp::Packet) -> u16 { udp_hdr } - EtherType::Arp => unreachable!(), + _ => unreachable!(), }; udp_hdr.check = 0; @@ -152,7 +152,7 @@ fn csum_ep(packet: &mut xdp::Packet) -> u16 { .unwrap() .to_be() } - EtherType::Arp => unreachable!(), + _ => unreachable!(), } } diff --git a/src/packet/csum.rs b/src/packet/csum.rs index 6e11ea9..255eeab 100644 --- a/src/packet/csum.rs +++ b/src/packet/csum.rs @@ -224,9 +224,9 @@ use crate::packet::net_types as nt; #[derive(Debug)] pub enum UdpCalcError { /// Not an IP packet - NotIp(nt::EtherType), + NotIp(nt::EtherType::Enum), /// Not a UDP packet - NotUdp(nt::IpProto), + NotUdp(nt::IpProto::Enum), /// Packet data was invalid/corrupt Packet(super::PacketError), } @@ -350,7 +350,7 @@ impl super::Packet { (finalize(sum), udp_hdr) } } - EtherType::Arp => return Err(UdpCalcError::NotIp(EtherType::Arp)), + invalid => return Err(UdpCalcError::NotIp(invalid)), }; let checksum = if self.can_offload_checksum() { diff --git a/src/packet/net_types.rs b/src/packet/net_types.rs index 4247ea0..c5c0078 100644 --- a/src/packet/net_types.rs +++ b/src/packet/net_types.rs @@ -90,7 +90,7 @@ pub struct EthHdr { /// The source MAC address pub source: MacAddress, /// The [`EtherType`] determines the rest of the payload - pub ether_type: EtherType, + pub ether_type: EtherType::Enum, } len!(EthHdr); @@ -108,321 +108,40 @@ impl EthHdr { } /// The [payload](https://en.wikipedia.org/wiki/EtherType) for an Ethernet frame -#[repr(u16)] -#[derive(Debug, PartialEq, Eq, Copy, Clone)] -pub enum EtherType { +#[allow(non_snake_case, non_upper_case_globals)] +pub mod EtherType { + /// The `EtherType` repr + pub type Enum = u16; + /// The payload is an [`Ipv4Hdr`] - Ipv4 = 0x0800_u16.to_be(), + pub const Ipv4: Enum = 0x0800_u16.to_be(); /// [Address Resolution Protocol](https://en.wikipedia.org/wiki/Address_Resolution_Protocol) - Arp = 0x0806_u16.to_be(), + pub const Arp: Enum = 0x0806_u16.to_be(); /// The payload is an [`Ipv6Hdr`] - Ipv6 = 0x86DD_u16.to_be(), + pub const Ipv6: Enum = 0x86dd_u16.to_be(); } /// Various transport layer protocols that can be encapsulated in an IPv4 or IPv6 /// packet /// /// -#[repr(u8)] -#[derive(PartialEq, Eq, Debug, Copy, Clone)] -#[allow(clippy::doc_markdown)] -pub enum IpProto { - /// IPv6 Hop-by-Hop Option - HopOpt = 0, +#[allow(non_snake_case, non_upper_case_globals)] +pub mod IpProto { + /// The `IpProto` repr + pub type Enum = u8; + /// Internet Control Message - Icmp = 1, + pub const Icmp: Enum = 1; /// Internet Group Management - Igmp = 2, - /// Gateway-to-Gateway - Ggp = 3, - /// IPv4 encapsulation - Ipv4 = 4, - /// Stream - Stream = 5, + pub const Igmp: Enum = 2; /// Transmission Control - Tcp = 6, - /// CBT - Cbt = 7, - /// Exterior Gateway Protocol - Egp = 8, - /// Any private interior gateway (used by Cisco for their IGRP) - Igp = 9, - /// BBN RCC Monitoring - BbnRccMon = 10, - /// Network Voice Protocol - NvpII = 11, - /// PUP - Pup = 12, - /// ARGUS - Argus = 13, - /// EMCON - Emcon = 14, - /// Cross Net Debugger - Xnet = 15, - /// Chaos - Chaos = 16, + pub const Tcp: Enum = 6; /// [User Datagram](struct@UdpHdr) - Udp = 17, - /// Multiplexing - Mux = 18, - /// DCN Measurement Subsystems - DcnMeas = 19, - /// Host Monitoring - Hmp = 20, - /// Packet Radio Measurement - Prm = 21, - /// XEROX NS IDP - Idp = 22, - /// Trunk-1 - Trunk1 = 23, - /// Trunk-2 - Trunk2 = 24, - /// Leaf-1 - Leaf1 = 25, - /// Leaf-2 - Leaf2 = 26, - /// Reliable Data Protocol - Rdp = 27, - /// Internet Reliable Transaction - Irtp = 28, - /// ISO Transport Protocol Class 4 - Tp4 = 29, - /// Bulk Data Transfer Protocol - Netblt = 30, - /// MFE Network Services Protocol - MfeNsp = 31, - /// MERIT Internodal Protocol - MeritInp = 32, - /// Datagram Congestion Control Protocol - Dccp = 33, - /// Third Party Connect Protocol - ThirdPartyConnect = 34, - /// Inter-Domain Policy Routing Protocol - Idpr = 35, - /// XTP - Xtp = 36, - /// Datagram Delivery Protocol - Ddp = 37, - /// IDPR Control Message Transport Proto - IdprCmtp = 38, - /// TP++ Transport Protocol - TpPlusPlus = 39, - /// IL Transport Protocol - Il = 40, - /// IPv6 encapsulation - Ipv6 = 41, - /// Source Demand Routing Protocol - Sdrp = 42, - /// Routing Header for IPv6 - Ipv6Route = 43, - /// Fragment Header for IPv6 - Ipv6Frag = 44, - /// Inter-Domain Routing Protocol - Idrp = 45, - /// Reservation Protocol - Rsvp = 46, - /// General Routing Encapsulation - Gre = 47, - /// Dynamic Source Routing Protocol - Dsr = 48, - /// BNA - Bna = 49, - /// Encap Security Payload - Esp = 50, - /// Authentication Header - Ah = 51, - /// Integrated Net Layer Security TUBA - Inlsp = 52, - /// IP with Encryption - Swipe = 53, - /// NBMA Address Resolution Protocol - Narp = 54, - /// IP Mobility - Mobile = 55, - /// Transport Layer Security Protocol using Kryptonet key management - Tlsp = 56, - /// SKIP - Skip = 57, + pub const Udp: Enum = 17; /// Internet Control Message Protocol for IPv6 - Ipv6Icmp = 58, - /// No Next Header for IPv6 - Ipv6NoNxt = 59, - /// Destination Options for IPv6 - Ipv6Opts = 60, - /// Any host internal protocol - AnyHostInternal = 61, - /// CFTP - Cftp = 62, - /// Any local network - AnyLocalNetwork = 63, - /// SATNET and Backroom EXPAK - SatExpak = 64, - /// Kryptolan - Kryptolan = 65, - /// MIT Remote Virtual Disk Protocol - Rvd = 66, - /// Internet Pluribus Packet Core - Ippc = 67, - /// Any distributed file system - AnyDistributedFileSystem = 68, - /// SATNET Monitoring - SatMon = 69, - /// VISA Protocol - Visa = 70, - /// Internet Packet Core Utility - Ipcv = 71, - /// Computer Protocol Network Executive - Cpnx = 72, - /// Computer Protocol Heart Beat - Cphb = 73, - /// Wang Span Network - Wsn = 74, - /// Packet Video Protocol - Pvp = 75, - /// Backroom SATNET Monitoring - BrSatMon = 76, - /// SUN ND PROTOCOL-Temporary - SunNd = 77, - /// WIDEBAND Monitoring - WbMon = 78, - /// WIDEBAND EXPAK - WbExpak = 79, - /// ISO Internet Protocol - IsoIp = 80, - /// VMTP - Vmtp = 81, - /// SECURE-VMTP - SecureVmtp = 82, - /// VINES - Vines = 83, - /// Transaction Transport Protocol - Ttp = 84, - /// NSFNET-IGP - NsfnetIgp = 85, - /// Dissimilar Gateway Protocol - Dgp = 86, - /// TCF - Tcf = 87, - /// EIGRP - Eigrp = 88, - /// OSPFIGP - Ospfigp = 89, - /// Sprite RPC Protocol - SpriteRpc = 90, - /// Locus Address Resolution Protocol - Larp = 91, - /// Multicast Transport Protocol - Mtp = 92, - /// AX.25 Frames - Ax25 = 93, - /// IP-within-IP Encapsulation Protocol - Ipip = 94, - /// Mobile Internetworking Control Pro. - Micp = 95, - /// Semaphore Communications Sec. Pro. - SccSp = 96, - /// Ethernet-within-IP Encapsulation - Etherip = 97, - /// Encapsulation Header - Encap = 98, - /// Any private encryption scheme - AnyPrivateEncryptionScheme = 99, - /// GMTP - Gmtp = 100, - /// Ipsilon Flow Management Protocol - Ifmp = 101, - /// PNNI over IP - Pnni = 102, - /// Protocol Independent Multicast - Pim = 103, - /// ARIS - Aris = 104, - /// SCPS - Scps = 105, - /// QNX - Qnx = 106, - /// Active Networks - ActiveNetworks = 107, - /// IP Payload Compression Protocol - IpComp = 108, - /// Sitara Networks Protocol - Snp = 109, - /// Compaq Peer Protocol - CompaqPeer = 110, - /// IPX in IP - IpxInIp = 111, - /// Virtual Router Redundancy Protocol - Vrrp = 112, - /// PGM Reliable Transport Protocol - Pgm = 113, - /// Any 0-hop protocol - AnyZeroHopProtocol = 114, - /// Layer Two Tunneling Protocol - L2tp = 115, - /// D-II Data Exchange (DDX) - Ddx = 116, - /// Interactive Agent Transfer Protocol - Iatp = 117, - /// Schedule Transfer Protocol - Stp = 118, - /// SpectraLink Radio Protocol - Srp = 119, - /// UTI - Uti = 120, - /// Simple Message Protocol - Smp = 121, - /// Simple Multicast Protocol - Sm = 122, - /// Performance Transparency Protocol - Ptp = 123, - /// ISIS over IPv4 - IsisOverIpv4 = 124, - /// FIRE - Fire = 125, - /// Combat Radio Transport Protocol - Crtp = 126, - /// Combat Radio User Datagram - Crudp = 127, - /// SSCOPMCE - Sscopmce = 128, - /// IPLT - Iplt = 129, - /// Secure Packet Shield - Sps = 130, - /// Private IP Encapsulation within IP - Pipe = 131, - /// Stream Control Transmission Protocol - Sctp = 132, - /// Fibre Channel - Fc = 133, - /// RSVP-E2E-IGNORE - RsvpE2eIgnore = 134, - /// Mobility Header - MobilityHeader = 135, + pub const Ipv6Icmp: Enum = 58; /// Lightweight User Datagram Protocol - UdpLite = 136, - /// MPLS-in-IP - Mpls = 137, - /// MANET Protocols - Manet = 138, - /// Host Identity Protocol - Hip = 139, - /// Shim6 Protocol - Shim6 = 140, - /// Wrapped Encapsulating Security Payload - Wesp = 141, - /// Robust Header Compression - Rohc = 142, - /// Ethernet in IPv4 - EthernetInIpv4 = 143, - /// AGGFRAG encapsulation payload for ESP - Aggfrag = 144, - /// Use for experimentation and testing - Test1 = 253, - /// Use for experimentation and testing - Test2 = 254, - /// Reserved - Reserved = 255, + pub const UdpLite: Enum = 136; } /// The [IPv4](https://en.wikipedia.org/wiki/IPv4) header @@ -442,7 +161,7 @@ pub struct Ipv4Hdr { #[doc(alias = "ttl")] pub time_to_live: u8, /// The layer 4 protocol encapsulated in this packet - pub proto: IpProto, + pub proto: IpProto::Enum, /// The [checksum](https://en.wikipedia.org/wiki/Internet_checksum) of the /// fields in this header, with the check field itself being 0 pub check: u16, @@ -455,7 +174,7 @@ pub struct Ipv4Hdr { impl Ipv4Hdr { /// Zeroes out the header #[inline] - pub fn reset(&mut self, ttl: u8, proto: IpProto) { + pub fn reset(&mut self, ttl: u8, proto: IpProto::Enum) { *self = Self::zeroed(); self.bitfield = 0x0045; self.time_to_live = ttl; @@ -516,7 +235,7 @@ pub struct Ipv6Hdr { pub payload_length: NetworkU16, /// The next header, usually the transport layer protocol, but could be one /// or more [extension headers](https://en.wikipedia.org/wiki/IPv6_packet#Extension_headers) - pub next_header: IpProto, + pub next_header: IpProto::Enum, /// The equivalent of [`Ipv4Hdr::time_to_live`]. /// /// This value is decremented by one at each forwarding node and the packet @@ -532,7 +251,7 @@ pub struct Ipv6Hdr { impl Ipv6Hdr { /// Zeroes out the header #[inline] - pub fn reset(&mut self, hop: u8, proto: IpProto) { + pub fn reset(&mut self, hop: u8, proto: IpProto::Enum) { *self = Self::zeroed(); self.bitfield = 0x00000060; @@ -907,7 +626,7 @@ impl UdpHeaders { return Ok(None); } } - EtherType::Arp => { + _ => { return Ok(None); } }; From a160d1e9a0fc5d5baf9c97ce3e77736dda5859b0 Mon Sep 17 00:00:00 2001 From: Jake Shadle Date: Tue, 4 Mar 2025 22:37:09 +0100 Subject: [PATCH 2/2] Fix docs --- src/packet/net_types.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/packet/net_types.rs b/src/packet/net_types.rs index c5c0078..cdcd34d 100644 --- a/src/packet/net_types.rs +++ b/src/packet/net_types.rs @@ -113,11 +113,11 @@ pub mod EtherType { /// The `EtherType` repr pub type Enum = u16; - /// The payload is an [`Ipv4Hdr`] + /// The payload is an [`super::Ipv4Hdr`] pub const Ipv4: Enum = 0x0800_u16.to_be(); /// [Address Resolution Protocol](https://en.wikipedia.org/wiki/Address_Resolution_Protocol) pub const Arp: Enum = 0x0806_u16.to_be(); - /// The payload is an [`Ipv6Hdr`] + /// The payload is an [`super::Ipv6Hdr`] pub const Ipv6: Enum = 0x86dd_u16.to_be(); } @@ -136,7 +136,7 @@ pub mod IpProto { pub const Igmp: Enum = 2; /// Transmission Control pub const Tcp: Enum = 6; - /// [User Datagram](struct@UdpHdr) + /// [User Datagram](struct@super::UdpHdr) pub const Udp: Enum = 17; /// Internet Control Message Protocol for IPv6 pub const Ipv6Icmp: Enum = 58;