1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! Ethertype Information Element
//!
//! The Ethertype IE contains the Ethernet frame type field for packet filtering.
//! Per 3GPP TS 29.244 Section 8.2.96, this IE is used in Ethernet packet filtering scenarios.
//!
//! Common ethertypes include IPv4 (0x0800), IPv6 (0x86DD), ARP (0x0806), VLAN (0x8100).
use crate::error::PfcpError;
use crate::ie::{Ie, IeType};
/// Ethertype
///
/// Represents the Ethernet frame type field for packet classification.
///
/// # 3GPP Reference
/// 3GPP TS 29.244 Section 8.2.96
///
/// # Structure
/// - 2 octets: Ethertype value in network byte order
///
/// # Common Ethertypes
/// - 0x0800: IPv4
/// - 0x0806: ARP
/// - 0x8100: VLAN-tagged frame (IEEE 802.1Q)
/// - 0x86DD: IPv6
/// - 0x8847: MPLS unicast
/// - 0x8848: MPLS multicast
///
/// # Examples
///
/// ```
/// use rs_pfcp::ie::ethertype::Ethertype;
///
/// // Create Ethertype for IPv4
/// let ipv4 = Ethertype::new(0x0800);
/// assert_eq!(ipv4.value(), 0x0800);
///
/// // Create Ethertype for IPv6
/// let ipv6 = Ethertype::new(0x86DD);
/// assert_eq!(ipv6.value(), 0x86DD);
///
/// // Marshal and unmarshal
/// let bytes = ipv4.marshal();
/// let parsed = Ethertype::unmarshal(&bytes).unwrap();
/// assert_eq!(ipv4, parsed);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Ethertype {
/// Ethertype value (16-bit)
value: u16,
}
impl Ethertype {
/// IPv4 ethertype (0x0800)
pub const IPV4: u16 = 0x0800;
/// ARP ethertype (0x0806)
pub const ARP: u16 = 0x0806;
/// VLAN-tagged frame (0x8100)
pub const VLAN: u16 = 0x8100;
/// IPv6 ethertype (0x86DD)
pub const IPV6: u16 = 0x86DD;
/// MPLS unicast (0x8847)
pub const MPLS_UNICAST: u16 = 0x8847;
/// MPLS multicast (0x8848)
pub const MPLS_MULTICAST: u16 = 0x8848;
/// Create a new Ethertype
///
/// # Arguments
/// * `value` - Ethertype value (16-bit)
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethertype::Ethertype;
///
/// let ethertype = Ethertype::new(0x0800); // IPv4
/// assert_eq!(ethertype.value(), 0x0800);
/// ```
pub fn new(value: u16) -> Self {
Ethertype { value }
}
/// Create IPv4 ethertype
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethertype::Ethertype;
///
/// let ipv4 = Ethertype::ipv4();
/// assert_eq!(ipv4.value(), 0x0800);
/// ```
pub fn ipv4() -> Self {
Ethertype::new(Self::IPV4)
}
/// Create IPv6 ethertype
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethertype::Ethertype;
///
/// let ipv6 = Ethertype::ipv6();
/// assert_eq!(ipv6.value(), 0x86DD);
/// ```
pub fn ipv6() -> Self {
Ethertype::new(Self::IPV6)
}
/// Create ARP ethertype
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethertype::Ethertype;
///
/// let arp = Ethertype::arp();
/// assert_eq!(arp.value(), 0x0806);
/// ```
pub fn arp() -> Self {
Ethertype::new(Self::ARP)
}
/// Get the ethertype value
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethertype::Ethertype;
///
/// let ethertype = Ethertype::new(0x8100);
/// assert_eq!(ethertype.value(), 0x8100);
/// ```
pub fn value(&self) -> u16 {
self.value
}
/// Marshal Ethertype to bytes
///
/// # Returns
/// 2-byte array with ethertype in network byte order
pub fn marshal(&self) -> [u8; 2] {
self.value.to_be_bytes()
}
/// Unmarshal Ethertype from bytes
///
/// # Arguments
/// * `data` - Byte slice containing Ethertype data (must be at least 2 bytes)
///
/// # Errors
/// Returns error if data is too short
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethertype::Ethertype;
///
/// let ethertype = Ethertype::new(0x0800);
/// let bytes = ethertype.marshal();
/// let parsed = Ethertype::unmarshal(&bytes).unwrap();
/// assert_eq!(ethertype, parsed);
/// ```
pub fn unmarshal(data: &[u8]) -> Result<Self, PfcpError> {
if data.len() < 2 {
return Err(PfcpError::invalid_length(
"Ethertype",
IeType::Ethertype,
2,
data.len(),
));
}
let value = u16::from_be_bytes([data[0], data[1]]);
Ok(Ethertype { value })
}
/// Convert to generic IE
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethertype::Ethertype;
/// use rs_pfcp::ie::IeType;
///
/// let ethertype = Ethertype::ipv4();
/// let ie = ethertype.to_ie();
/// assert_eq!(ie.ie_type, IeType::Ethertype);
/// ```
pub fn to_ie(&self) -> Ie {
Ie::new(IeType::Ethertype, self.marshal().to_vec())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ethertype_new() {
let ethertype = Ethertype::new(0x0800);
assert_eq!(ethertype.value(), 0x0800);
}
#[test]
fn test_ethertype_ipv4() {
let ipv4 = Ethertype::ipv4();
assert_eq!(ipv4.value(), 0x0800);
}
#[test]
fn test_ethertype_ipv6() {
let ipv6 = Ethertype::ipv6();
assert_eq!(ipv6.value(), 0x86DD);
}
#[test]
fn test_ethertype_arp() {
let arp = Ethertype::arp();
assert_eq!(arp.value(), 0x0806);
}
#[test]
fn test_ethertype_marshal() {
let ethertype = Ethertype::new(0x0800);
let bytes = ethertype.marshal();
assert_eq!(bytes.len(), 2);
assert_eq!(bytes, [0x08, 0x00]);
let ethertype2 = Ethertype::new(0x86DD);
let bytes2 = ethertype2.marshal();
assert_eq!(bytes2, [0x86, 0xDD]);
}
#[test]
fn test_ethertype_unmarshal_valid() {
let data = [0x08, 0x00]; // IPv4
let ethertype = Ethertype::unmarshal(&data).unwrap();
assert_eq!(ethertype.value(), 0x0800);
let data2 = [0x86, 0xDD]; // IPv6
let ethertype2 = Ethertype::unmarshal(&data2).unwrap();
assert_eq!(ethertype2.value(), 0x86DD);
}
#[test]
fn test_ethertype_unmarshal_short() {
let data = [0x08];
let result = Ethertype::unmarshal(&data);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(matches!(err, PfcpError::InvalidLength { .. }));
}
#[test]
fn test_ethertype_unmarshal_empty() {
let result = Ethertype::unmarshal(&[]);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(matches!(err, PfcpError::InvalidLength { .. }));
assert!(err.to_string().contains("Ethertype"));
assert!(err.to_string().contains("2"));
assert!(err.to_string().contains("0"));
}
#[test]
fn test_ethertype_round_trip() {
let test_cases = vec![
0x0800, // IPv4
0x0806, // ARP
0x8100, // VLAN
0x86DD, // IPv6
0x8847, // MPLS unicast
0x8848, // MPLS multicast
0xFFFF, // Max value
0x0000, // Min value
];
for value in test_cases {
let original = Ethertype::new(value);
let marshaled = original.marshal();
let unmarshaled = Ethertype::unmarshal(&marshaled).unwrap();
assert_eq!(
original, unmarshaled,
"Failed for ethertype 0x{:04X}",
value
);
}
}
#[test]
fn test_ethertype_to_ie() {
let ethertype = Ethertype::ipv4();
let ie = ethertype.to_ie();
assert_eq!(ie.ie_type, IeType::Ethertype);
assert_eq!(ie.payload.len(), 2);
assert_eq!(ie.payload, vec![0x08, 0x00]);
// Verify IE can be unmarshaled
let parsed = Ethertype::unmarshal(&ie.payload).unwrap();
assert_eq!(ethertype, parsed);
}
#[test]
fn test_ethertype_scenarios() {
// Scenario 1: IPv4 traffic filtering
let ipv4_filter = Ethertype::ipv4();
assert_eq!(ipv4_filter.value(), 0x0800);
// Scenario 2: IPv6 traffic filtering
let ipv6_filter = Ethertype::ipv6();
assert_eq!(ipv6_filter.value(), 0x86DD);
// Scenario 3: ARP filtering
let arp_filter = Ethertype::arp();
assert_eq!(arp_filter.value(), 0x0806);
// Scenario 4: VLAN-tagged frames
let vlan_filter = Ethertype::new(Ethertype::VLAN);
assert_eq!(vlan_filter.value(), 0x8100);
// Scenario 5: MPLS traffic
let mpls_filter = Ethertype::new(Ethertype::MPLS_UNICAST);
assert_eq!(mpls_filter.value(), 0x8847);
}
#[test]
fn test_ethertype_constants() {
assert_eq!(Ethertype::IPV4, 0x0800);
assert_eq!(Ethertype::ARP, 0x0806);
assert_eq!(Ethertype::VLAN, 0x8100);
assert_eq!(Ethertype::IPV6, 0x86DD);
assert_eq!(Ethertype::MPLS_UNICAST, 0x8847);
assert_eq!(Ethertype::MPLS_MULTICAST, 0x8848);
}
#[test]
fn test_ethertype_clone_copy() {
let eth1 = Ethertype::new(0x0800);
let eth2 = eth1;
assert_eq!(eth1, eth2);
let eth3 = eth1;
assert_eq!(eth1, eth3);
}
}