rs-pfcp 0.4.0

High-performance Rust implementation of PFCP (Packet Forwarding Control Protocol) for 5G networks with 100% 3GPP TS 29.244 Release 18 compliance
Documentation
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Trace Information IE.

use crate::error::PfcpError;
use crate::ie::{Ie, IeType};

/// Represents the Trace Information Element.
/// Used for network debugging and tracing support in 5G networks.
/// Defined in 3GPP TS 29.244 Section 8.2.102.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraceInformation {
    pub mcc_mnc: [u8; 3],  // Mobile Country Code and Mobile Network Code (PLMN ID)
    pub trace_id: [u8; 3], // Trace ID (24 bits)
    pub triggering_events: Vec<u8>, // List of triggering events (variable length)
    pub trace_depth: u8,   // Trace depth
    pub list_of_interfaces: Vec<u8>, // List of interfaces to trace (variable length)
    pub ip_address_of_trace_collection_entity: Option<Vec<u8>>, // IPv4 or IPv6 address
}

impl TraceInformation {
    /// Creates a new Trace Information IE.
    pub fn new(
        mcc_mnc: [u8; 3],
        trace_id: [u8; 3],
        triggering_events: Vec<u8>,
        trace_depth: u8,
        list_of_interfaces: Vec<u8>,
    ) -> Self {
        TraceInformation {
            mcc_mnc,
            trace_id,
            triggering_events,
            trace_depth,
            list_of_interfaces,
            ip_address_of_trace_collection_entity: None,
        }
    }

    /// Adds an IP address of the trace collection entity.
    pub fn with_trace_collection_entity_ip(mut self, ip_address: Vec<u8>) -> Self {
        self.ip_address_of_trace_collection_entity = Some(ip_address);
        self
    }

    /// Sets the trace collection entity IPv4 address.
    pub fn with_trace_collection_entity_ipv4(mut self, ipv4: std::net::Ipv4Addr) -> Self {
        self.ip_address_of_trace_collection_entity = Some(ipv4.octets().to_vec());
        self
    }

    /// Sets the trace collection entity IPv6 address.
    pub fn with_trace_collection_entity_ipv6(mut self, ipv6: std::net::Ipv6Addr) -> Self {
        self.ip_address_of_trace_collection_entity = Some(ipv6.octets().to_vec());
        self
    }

    /// Gets the PLMN ID (MCC + MNC).
    pub fn plmn_id(&self) -> [u8; 3] {
        self.mcc_mnc
    }

    /// Gets the trace ID.
    pub fn trace_id(&self) -> [u8; 3] {
        self.trace_id
    }

    /// Gets the trace collection entity IP address as IPv4 if possible.
    pub fn trace_collection_entity_ipv4(&self) -> Option<std::net::Ipv4Addr> {
        if let Some(ref ip) = self.ip_address_of_trace_collection_entity {
            if ip.len() == 4 {
                return Some(std::net::Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]));
            }
        }
        None
    }

    /// Gets the trace collection entity IP address as IPv6 if possible.
    pub fn trace_collection_entity_ipv6(&self) -> Option<std::net::Ipv6Addr> {
        if let Some(ref ip) = self.ip_address_of_trace_collection_entity {
            if ip.len() == 16 {
                let mut octets = [0u8; 16];
                octets.copy_from_slice(ip);
                return Some(std::net::Ipv6Addr::from(octets));
            }
        }
        None
    }

    /// Gets the length of the marshaled Trace Information.
    pub fn len(&self) -> usize {
        let mut len = 3 + 3 + 1 + 1; // mcc_mnc + trace_id + triggering_events_len + trace_depth
        len += self.triggering_events.len();
        len += 1; // list_of_interfaces_len
        len += self.list_of_interfaces.len();

        if let Some(ref ip) = self.ip_address_of_trace_collection_entity {
            len += 1 + ip.len(); // ip_address_len + ip_address
        } else {
            len += 1; // ip_address_len = 0
        }

        len
    }

    /// Checks if the Trace Information is empty.
    pub fn is_empty(&self) -> bool {
        false // Trace Information always has mandatory fields
    }

    /// Marshals the Trace Information into a byte vector.
    pub fn marshal(&self) -> Vec<u8> {
        let mut data = Vec::new();

        // PLMN ID (MCC + MNC)
        data.extend_from_slice(&self.mcc_mnc);

        // Trace ID
        data.extend_from_slice(&self.trace_id);

        // Triggering Events length and data
        data.push(self.triggering_events.len() as u8);
        data.extend_from_slice(&self.triggering_events);

        // Trace Depth
        data.push(self.trace_depth);

        // List of Interfaces length and data
        data.push(self.list_of_interfaces.len() as u8);
        data.extend_from_slice(&self.list_of_interfaces);

        // IP Address of Trace Collection Entity length and data
        if let Some(ref ip) = self.ip_address_of_trace_collection_entity {
            data.push(ip.len() as u8);
            data.extend_from_slice(ip);
        } else {
            data.push(0); // No IP address
        }

        data
    }

    /// Unmarshals a byte slice into a Trace Information IE.
    pub fn unmarshal(payload: &[u8]) -> Result<Self, PfcpError> {
        if payload.len() < 9 {
            return Err(PfcpError::invalid_length(
                "Trace Information",
                IeType::TraceInformation,
                9,
                payload.len(),
            ));
        }

        let mut offset = 0;

        // PLMN ID (MCC + MNC)
        let mut mcc_mnc = [0u8; 3];
        mcc_mnc.copy_from_slice(&payload[offset..offset + 3]);
        offset += 3;

        // Trace ID
        let mut trace_id = [0u8; 3];
        trace_id.copy_from_slice(&payload[offset..offset + 3]);
        offset += 3;

        // Triggering Events length
        let triggering_events_len = payload[offset] as usize;
        offset += 1;

        if offset + triggering_events_len > payload.len() {
            return Err(PfcpError::invalid_value(
                "triggering_events_len",
                triggering_events_len.to_string(),
                "length exceeds remaining payload",
            ));
        }

        // Triggering Events data
        let triggering_events = payload[offset..offset + triggering_events_len].to_vec();
        offset += triggering_events_len;

        if offset >= payload.len() {
            return Err(PfcpError::invalid_length(
                "Trace Information (trace_depth)",
                IeType::TraceInformation,
                offset + 1,
                payload.len(),
            ));
        }

        // Trace Depth
        let trace_depth = payload[offset];
        offset += 1;

        if offset >= payload.len() {
            return Err(PfcpError::invalid_length(
                "Trace Information (interfaces_len)",
                IeType::TraceInformation,
                offset + 1,
                payload.len(),
            ));
        }

        // List of Interfaces length
        let interfaces_len = payload[offset] as usize;
        offset += 1;

        if offset + interfaces_len > payload.len() {
            return Err(PfcpError::invalid_value(
                "interfaces_len",
                interfaces_len.to_string(),
                "length exceeds remaining payload",
            ));
        }

        // List of Interfaces data
        let list_of_interfaces = payload[offset..offset + interfaces_len].to_vec();
        offset += interfaces_len;

        if offset >= payload.len() {
            return Err(PfcpError::invalid_length(
                "Trace Information (ip_len)",
                IeType::TraceInformation,
                offset + 1,
                payload.len(),
            ));
        }

        // IP Address of Trace Collection Entity length
        let ip_len = payload[offset] as usize;
        offset += 1;

        let ip_address_of_trace_collection_entity = if ip_len > 0 {
            if offset + ip_len > payload.len() {
                return Err(PfcpError::invalid_value(
                    "ip_len",
                    ip_len.to_string(),
                    "length exceeds remaining payload",
                ));
            }
            Some(payload[offset..offset + ip_len].to_vec())
        } else {
            None
        };

        Ok(TraceInformation {
            mcc_mnc,
            trace_id,
            triggering_events,
            trace_depth,
            list_of_interfaces,
            ip_address_of_trace_collection_entity,
        })
    }

    /// Wraps the Trace Information in a Trace Information IE.
    pub fn to_ie(&self) -> Ie {
        Ie::new(IeType::TraceInformation, self.marshal())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{Ipv4Addr, Ipv6Addr};

    #[test]
    fn test_trace_information_marshal_unmarshal_minimal() {
        let mcc_mnc = [0x12, 0x34, 0x56];
        let trace_id = [0xAB, 0xCD, 0xEF];
        let triggering_events = vec![0x01, 0x02];
        let trace_depth = 5;
        let list_of_interfaces = vec![0x10, 0x20, 0x30];

        let trace_info = TraceInformation::new(
            mcc_mnc,
            trace_id,
            triggering_events.clone(),
            trace_depth,
            list_of_interfaces.clone(),
        );

        let marshaled = trace_info.marshal();
        let unmarshaled = TraceInformation::unmarshal(&marshaled).unwrap();

        assert_eq!(trace_info, unmarshaled);
        assert_eq!(unmarshaled.mcc_mnc, mcc_mnc);
        assert_eq!(unmarshaled.trace_id, trace_id);
        assert_eq!(unmarshaled.triggering_events, triggering_events);
        assert_eq!(unmarshaled.trace_depth, trace_depth);
        assert_eq!(unmarshaled.list_of_interfaces, list_of_interfaces);
        assert_eq!(unmarshaled.ip_address_of_trace_collection_entity, None);
        assert!(!trace_info.is_empty());
    }

    #[test]
    fn test_trace_information_marshal_unmarshal_with_ipv4() {
        let mcc_mnc = [0x12, 0x34, 0x56];
        let trace_id = [0xAB, 0xCD, 0xEF];
        let triggering_events = vec![0x01];
        let trace_depth = 3;
        let list_of_interfaces = vec![0x10];
        let ipv4 = Ipv4Addr::new(192, 168, 1, 100);

        let trace_info = TraceInformation::new(
            mcc_mnc,
            trace_id,
            triggering_events.clone(),
            trace_depth,
            list_of_interfaces.clone(),
        )
        .with_trace_collection_entity_ipv4(ipv4);

        let marshaled = trace_info.marshal();
        let unmarshaled = TraceInformation::unmarshal(&marshaled).unwrap();

        assert_eq!(trace_info, unmarshaled);
        assert_eq!(unmarshaled.trace_collection_entity_ipv4(), Some(ipv4));
        assert_eq!(unmarshaled.trace_collection_entity_ipv6(), None);
    }

    #[test]
    fn test_trace_information_marshal_unmarshal_with_ipv6() {
        let mcc_mnc = [0x12, 0x34, 0x56];
        let trace_id = [0xAB, 0xCD, 0xEF];
        let triggering_events = vec![0x01, 0x02, 0x03];
        let trace_depth = 7;
        let list_of_interfaces = vec![0x10, 0x20];
        let ipv6 = Ipv6Addr::new(0x2001, 0xdb8, 0x85a3, 0, 0, 0x8a2e, 0x370, 0x7334);

        let trace_info = TraceInformation::new(
            mcc_mnc,
            trace_id,
            triggering_events.clone(),
            trace_depth,
            list_of_interfaces.clone(),
        )
        .with_trace_collection_entity_ipv6(ipv6);

        let marshaled = trace_info.marshal();
        let unmarshaled = TraceInformation::unmarshal(&marshaled).unwrap();

        assert_eq!(trace_info, unmarshaled);
        assert_eq!(unmarshaled.trace_collection_entity_ipv6(), Some(ipv6));
        assert_eq!(unmarshaled.trace_collection_entity_ipv4(), None);
    }

    #[test]
    fn test_trace_information_with_custom_ip() {
        let mcc_mnc = [0x01, 0x02, 0x03];
        let trace_id = [0x04, 0x05, 0x06];
        let triggering_events = vec![0xFF];
        let trace_depth = 1;
        let list_of_interfaces = vec![0xAA, 0xBB];
        let custom_ip = vec![0x10, 0x20, 0x30, 0x40, 0x50]; // 5-byte custom format

        let trace_info = TraceInformation::new(
            mcc_mnc,
            trace_id,
            triggering_events.clone(),
            trace_depth,
            list_of_interfaces.clone(),
        )
        .with_trace_collection_entity_ip(custom_ip.clone());

        let marshaled = trace_info.marshal();
        let unmarshaled = TraceInformation::unmarshal(&marshaled).unwrap();

        assert_eq!(trace_info, unmarshaled);
        assert_eq!(
            unmarshaled.ip_address_of_trace_collection_entity,
            Some(custom_ip)
        );
        assert_eq!(unmarshaled.trace_collection_entity_ipv4(), None);
        assert_eq!(unmarshaled.trace_collection_entity_ipv6(), None);
    }

    #[test]
    fn test_trace_information_plmn_and_trace_id() {
        let mcc_mnc = [0xAA, 0xBB, 0xCC];
        let trace_id = [0x11, 0x22, 0x33];
        let trace_info = TraceInformation::new(mcc_mnc, trace_id, vec![], 0, vec![]);

        assert_eq!(trace_info.plmn_id(), mcc_mnc);
        assert_eq!(trace_info.trace_id(), trace_id);
    }

    #[test]
    fn test_trace_information_to_ie() {
        let trace_info = TraceInformation::new(
            [0x12, 0x34, 0x56],
            [0xAB, 0xCD, 0xEF],
            vec![0x01],
            3,
            vec![0x10],
        );

        let ie = trace_info.to_ie();
        assert_eq!(ie.ie_type, IeType::TraceInformation);

        let unmarshaled = TraceInformation::unmarshal(&ie.payload).unwrap();
        assert_eq!(trace_info, unmarshaled);
    }

    #[test]
    fn test_trace_information_unmarshal_too_short() {
        let result = TraceInformation::unmarshal(&[0x01, 0x02]); // Too short
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PfcpError::InvalidLength { .. }
        ));
    }

    #[test]
    fn test_trace_information_unmarshal_invalid_triggering_events_len() {
        let mut payload = vec![0x12, 0x34, 0x56, 0xAB, 0xCD, 0xEF]; // mcc_mnc + trace_id
        payload.push(10); // triggering_events_len = 10, but not enough data
        payload.extend(vec![0x01, 0x02]); // Only 2 bytes of triggering events

        let result = TraceInformation::unmarshal(&payload);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PfcpError::InvalidValue { .. }
        ));
    }

    #[test]
    fn test_trace_information_unmarshal_missing_trace_depth() {
        let mut payload = vec![0x12, 0x34, 0x56, 0xAB, 0xCD, 0xEF]; // mcc_mnc + trace_id
        payload.push(2); // triggering_events_len = 2
        payload.extend(vec![0x01, 0x02]); // triggering events data
                                          // Missing trace_depth

        let result = TraceInformation::unmarshal(&payload);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PfcpError::InvalidLength { .. }
        ));
    }

    #[test]
    fn test_trace_information_len() {
        let trace_info_minimal = TraceInformation::new([0; 3], [0; 3], vec![], 0, vec![]);
        assert_eq!(trace_info_minimal.len(), 10); // 3+3+1+0+1+1+0+1+0 = 10

        let trace_info_with_data = TraceInformation::new(
            [0; 3],
            [0; 3],
            vec![0x01, 0x02], // 2 bytes
            0,
            vec![0x10, 0x20, 0x30], // 3 bytes
        )
        .with_trace_collection_entity_ipv4(Ipv4Addr::new(192, 168, 1, 1)); // 4 bytes

        assert_eq!(trace_info_with_data.len(), 19); // 3+3+1+2+1+1+3+1+4 = 19
    }

    #[test]
    fn test_trace_information_round_trip_empty_lists() {
        let trace_info = TraceInformation::new([0xFF; 3], [0xAA; 3], vec![], 255, vec![]);

        let marshaled = trace_info.marshal();
        let unmarshaled = TraceInformation::unmarshal(&marshaled).unwrap();

        assert_eq!(trace_info, unmarshaled);
        assert!(unmarshaled.triggering_events.is_empty());
        assert!(unmarshaled.list_of_interfaces.is_empty());
        assert_eq!(unmarshaled.ip_address_of_trace_collection_entity, None);
    }
}