mutnet/typed_protocol_headers/
ether_type.rs1#[repr(u16)]
8#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
9#[cfg_attr(kani, derive(kani::Arbitrary))]
10pub enum EtherType {
11 Ipv4 = 0x0800,
13 Arp = 0x0806,
15 WakeOnLan = 0x0842,
17 Avtp = 0x22F0,
19 Srp = 0x22EA,
21 Rarp = 0x8035,
23 AppleTalk = 0x809B,
25 Aarp = 0x80F3,
27 CustomerTag = 0x8100,
29 Slpp = 0x8102,
31 Vlacp = 0x8103,
33 Ipx = 0x8137,
35 QnxQnet = 0x8204,
37 Ipv6 = 0x86DD,
39 EthernetFlowControl = 0x8808,
41 EthernetSlowProtocols = 0x8809,
43 CobraNet = 0x8819,
45 MplsUnicast = 0x8847,
47 MplsMulticast = 0x8848,
49 PppoeDiscoveryStage = 0x8863,
51 PppoeSessionStage = 0x8864,
53 HomePlug1_0Mme = 0x887B,
55 EapOverLan = 0x888E,
57 Profinet = 0x8892,
59 HyperScsi = 0x889A,
61 AtaOverEthernet = 0x88A2,
63 EtherCat = 0x88A4,
65 ServiceTag = 0x88A8,
67 EthernetPowerlink = 0x88AB,
69 Goose = 0x88B8,
71 GseManagementServices = 0x88B9,
73 Sv = 0x88BA,
75 Lldp = 0x88CC,
77 Sercos3 = 0x88CD,
79 HomePlugGreenPhy = 0x88E1,
81 MediaRedundancyProtocol = 0x88E3,
83 MacSec = 0x88E5,
85 Pbb = 0x88E7,
87 Ptp = 0x88F7,
89 NcSi = 0x88F8,
91 Prp = 0x88FB,
93 Fcoe = 0x8906,
95 Mediaxtream = 0x8912,
97 FcoeInitializationProtocol = 0x8914,
99 Roce = 0x8915,
101 Tte = 0x891D,
103 Hsr = 0x892F,
105 EthernetConfigurationTestingProtocol = 0x9000,
107 RTag = 0xF1C1,
109}
110
111impl core::fmt::Display for EtherType {
112 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
113 write!(f, "{self:?}")
114 }
115}
116
117#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
119pub struct UnrecognizedEtherTypeError {
120 pub ether_type: u16,
122}
123
124impl core::fmt::Display for UnrecognizedEtherTypeError {
125 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
126 write!(f, "Unrecognized ether type, was: {:?}", self.ether_type)
127 }
128}
129
130impl core::error::Error for UnrecognizedEtherTypeError {}
131
132impl TryFrom<u16> for EtherType {
133 type Error = UnrecognizedEtherTypeError;
134 #[inline]
135 fn try_from(value: u16) -> Result<Self, Self::Error> {
136 match value {
137 0x0800 => Ok(EtherType::Ipv4),
138 0x0806 => Ok(EtherType::Arp),
139 0x0842 => Ok(EtherType::WakeOnLan),
140 0x22F0 => Ok(EtherType::Avtp),
141 0x22EA => Ok(EtherType::Srp),
142 0x8035 => Ok(EtherType::Rarp),
143 0x809B => Ok(EtherType::AppleTalk),
144 0x80F3 => Ok(EtherType::Aarp),
145 0x8100 => Ok(EtherType::CustomerTag),
146 0x8102 => Ok(EtherType::Slpp),
147 0x8103 => Ok(EtherType::Vlacp),
148 0x8137 => Ok(EtherType::Ipx),
149 0x8204 => Ok(EtherType::QnxQnet),
150 0x86DD => Ok(EtherType::Ipv6),
151 0x8808 => Ok(EtherType::EthernetFlowControl),
152 0x8809 => Ok(EtherType::EthernetSlowProtocols),
153 0x8819 => Ok(EtherType::CobraNet),
154 0x8847 => Ok(EtherType::MplsUnicast),
155 0x8848 => Ok(EtherType::MplsMulticast),
156 0x8863 => Ok(EtherType::PppoeDiscoveryStage),
157 0x8864 => Ok(EtherType::PppoeSessionStage),
158 0x887B => Ok(EtherType::HomePlug1_0Mme),
159 0x888E => Ok(EtherType::EapOverLan),
160 0x8892 => Ok(EtherType::Profinet),
161 0x889A => Ok(EtherType::HyperScsi),
162 0x88A2 => Ok(EtherType::AtaOverEthernet),
163 0x88A4 => Ok(EtherType::EtherCat),
164 0x88A8 => Ok(EtherType::ServiceTag),
165 0x88AB => Ok(EtherType::EthernetPowerlink),
166 0x88B8 => Ok(EtherType::Goose),
167 0x88B9 => Ok(EtherType::GseManagementServices),
168 0x88BA => Ok(EtherType::Sv),
169 0x88CC => Ok(EtherType::Lldp),
170 0x88CD => Ok(EtherType::Sercos3),
171 0x88E1 => Ok(EtherType::HomePlugGreenPhy),
172 0x88E3 => Ok(EtherType::MediaRedundancyProtocol),
173 0x88E5 => Ok(EtherType::MacSec),
174 0x88E7 => Ok(EtherType::Pbb),
175 0x88F7 => Ok(EtherType::Ptp),
176 0x88F8 => Ok(EtherType::NcSi),
177 0x88FB => Ok(EtherType::Prp),
178 0x8906 => Ok(EtherType::Fcoe),
179 0x8912 => Ok(EtherType::Mediaxtream),
180 0x8914 => Ok(EtherType::FcoeInitializationProtocol),
181 0x8915 => Ok(EtherType::Roce),
182 0x891D => Ok(EtherType::Tte),
183 0x892F => Ok(EtherType::Hsr),
184 0x9000 => Ok(EtherType::EthernetConfigurationTestingProtocol),
185 0xF1C1 => Ok(EtherType::RTag),
186
187 _ => Err(UnrecognizedEtherTypeError { ether_type: value }),
188 }
189 }
190}
191
192#[cfg(kani)]
194mod ether_type_verification {
195 use super::*;
196
197 #[kani::proof]
198 fn ether_type_proof() {
199 let try_value = kani::any::<u16>();
200 match EtherType::try_from(try_value) {
201 Ok(ether_type) => {
202 assert_eq!(ether_type as u16, try_value);
203 }
204 Err(err) => {
205 assert_eq!(
206 UnrecognizedEtherTypeError {
207 ether_type: try_value
208 },
209 err
210 );
211 }
212 }
213 }
214}