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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Outer Header Creation IE.
//!
//! This IE specifies the creation of GTP-U, UDP/IPv4, or UDP/IPv6 outer headers
//! for forwarding packets. Used in ForwardingParameters to configure tunnel endpoints.

use crate::error::PfcpError;
use crate::ie::{Ie, IeType};
use crate::types::Teid;
use std::net::{Ipv4Addr, Ipv6Addr};

/// Outer Header Creation Description flags
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OuterHeaderCreationFlags {
    pub gtpu_udp_ipv4: bool,
    pub gtpu_udp_ipv6: bool,
    pub udp_ipv4: bool,
    pub udp_ipv6: bool,
    pub ipv4: bool,
    pub ipv6: bool,
    pub ctag: bool, // C-TAG
    pub stag: bool, // S-TAG
}

impl OuterHeaderCreationFlags {
    pub fn new() -> Self {
        OuterHeaderCreationFlags {
            gtpu_udp_ipv4: false,
            gtpu_udp_ipv6: false,
            udp_ipv4: false,
            udp_ipv6: false,
            ipv4: false,
            ipv6: false,
            ctag: false,
            stag: false,
        }
    }

    /// GTP-U/UDP/IPv4 tunnel
    pub fn gtpu_ipv4() -> Self {
        OuterHeaderCreationFlags {
            gtpu_udp_ipv4: true,
            ..Self::new()
        }
    }

    /// GTP-U/UDP/IPv6 tunnel
    pub fn gtpu_ipv6() -> Self {
        OuterHeaderCreationFlags {
            gtpu_udp_ipv6: true,
            ..Self::new()
        }
    }

    /// UDP/IPv4 encapsulation (no GTP-U)
    pub fn udp_ipv4() -> Self {
        OuterHeaderCreationFlags {
            udp_ipv4: true,
            ..Self::new()
        }
    }

    /// UDP/IPv6 encapsulation (no GTP-U)
    pub fn udp_ipv6() -> Self {
        OuterHeaderCreationFlags {
            udp_ipv6: true,
            ..Self::new()
        }
    }

    fn to_u16(self) -> u16 {
        let mut flags = 0u16;
        if self.gtpu_udp_ipv4 {
            flags |= 0x0100; // Bit 8
        }
        if self.gtpu_udp_ipv6 {
            flags |= 0x0200; // Bit 9
        }
        if self.udp_ipv4 {
            flags |= 0x0400; // Bit 10
        }
        if self.udp_ipv6 {
            flags |= 0x0800; // Bit 11
        }
        if self.ipv4 {
            flags |= 0x1000; // Bit 12
        }
        if self.ipv6 {
            flags |= 0x2000; // Bit 13
        }
        if self.ctag {
            flags |= 0x4000; // Bit 14
        }
        if self.stag {
            flags |= 0x8000; // Bit 15
        }
        flags
    }

    fn from_u16(value: u16) -> Self {
        OuterHeaderCreationFlags {
            gtpu_udp_ipv4: (value & 0x0100) != 0,
            gtpu_udp_ipv6: (value & 0x0200) != 0,
            udp_ipv4: (value & 0x0400) != 0,
            udp_ipv6: (value & 0x0800) != 0,
            ipv4: (value & 0x1000) != 0,
            ipv6: (value & 0x2000) != 0,
            ctag: (value & 0x4000) != 0,
            stag: (value & 0x8000) != 0,
        }
    }
}

impl Default for OuterHeaderCreationFlags {
    fn default() -> Self {
        Self::new()
    }
}

/// Outer Header Creation IE
///
/// Specifies how to create outer headers for packet forwarding.
/// Commonly used for GTP-U tunnel setup in 5G networks.
///
/// # 3GPP TS 29.244 Reference
/// - Section 8.2.56: Outer Header Creation
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OuterHeaderCreation {
    pub description: OuterHeaderCreationFlags,
    pub teid: Option<Teid>, // GTP-U TEID
    pub ipv4_address: Option<Ipv4Addr>,
    pub ipv6_address: Option<Ipv6Addr>,
    pub port_number: Option<u16>, // UDP port
    pub ctag: Option<u32>,        // C-TAG (3 bytes)
    pub stag: Option<u32>,        // S-TAG (3 bytes)
}

impl OuterHeaderCreation {
    /// Creates a new Outer Header Creation IE
    pub fn new(description: OuterHeaderCreationFlags) -> Self {
        OuterHeaderCreation {
            description,
            teid: None,
            ipv4_address: None,
            ipv6_address: None,
            port_number: None,
            ctag: None,
            stag: None,
        }
    }

    /// Creates GTP-U/UDP/IPv4 tunnel configuration
    pub fn gtpu_ipv4(teid: impl Into<Teid>, ipv4: Ipv4Addr) -> Self {
        OuterHeaderCreation {
            description: OuterHeaderCreationFlags::gtpu_ipv4(),
            teid: Some(teid.into()),
            ipv4_address: Some(ipv4),
            ipv6_address: None,
            port_number: None,
            ctag: None,
            stag: None,
        }
    }

    /// Creates GTP-U/UDP/IPv6 tunnel configuration
    pub fn gtpu_ipv6(teid: impl Into<Teid>, ipv6: Ipv6Addr) -> Self {
        OuterHeaderCreation {
            description: OuterHeaderCreationFlags::gtpu_ipv6(),
            teid: Some(teid.into()),
            ipv4_address: None,
            ipv6_address: Some(ipv6),
            port_number: None,
            ctag: None,
            stag: None,
        }
    }

    /// Creates UDP/IPv4 encapsulation
    pub fn udp_ipv4(ipv4: Ipv4Addr, port: u16) -> Self {
        OuterHeaderCreation {
            description: OuterHeaderCreationFlags::udp_ipv4(),
            teid: None,
            ipv4_address: Some(ipv4),
            ipv6_address: None,
            port_number: Some(port),
            ctag: None,
            stag: None,
        }
    }

    /// Adds C-TAG (Customer VLAN tag)
    pub fn with_ctag(mut self, ctag: u32) -> Self {
        self.description.ctag = true;
        self.ctag = Some(ctag & 0xFFFFFF); // 3 bytes
        self
    }

    /// Adds S-TAG (Service VLAN tag)
    pub fn with_stag(mut self, stag: u32) -> Self {
        self.description.stag = true;
        self.stag = Some(stag & 0xFFFFFF); // 3 bytes
        self
    }

    /// Marshals the Outer Header Creation IE into bytes
    pub fn marshal(&self) -> Vec<u8> {
        let mut data = Vec::new();

        // Description (2 bytes, big-endian)
        let desc_value = self.description.to_u16();
        data.extend_from_slice(&desc_value.to_be_bytes());

        // TEID (4 bytes) - included if any GTP-U flag is set
        if self.description.gtpu_udp_ipv4 || self.description.gtpu_udp_ipv6 {
            let teid = self.teid.map(|t| t.0).unwrap_or(0);
            data.extend_from_slice(&teid.to_be_bytes());
        }

        // IPv4 Address (4 bytes)
        if self.description.gtpu_udp_ipv4 || self.description.udp_ipv4 || self.description.ipv4 {
            if let Some(ipv4) = self.ipv4_address {
                data.extend_from_slice(&ipv4.octets());
            }
        }

        // IPv6 Address (16 bytes)
        if self.description.gtpu_udp_ipv6 || self.description.udp_ipv6 || self.description.ipv6 {
            if let Some(ipv6) = self.ipv6_address {
                data.extend_from_slice(&ipv6.octets());
            }
        }

        // Port Number (2 bytes) - for UDP encapsulation
        if self.description.udp_ipv4 || self.description.udp_ipv6 {
            let port = self.port_number.unwrap_or(0);
            data.extend_from_slice(&port.to_be_bytes());
        }

        // C-TAG (3 bytes)
        if self.description.ctag {
            if let Some(ctag) = self.ctag {
                data.push(((ctag >> 16) & 0xFF) as u8);
                data.push(((ctag >> 8) & 0xFF) as u8);
                data.push((ctag & 0xFF) as u8);
            }
        }

        // S-TAG (3 bytes)
        if self.description.stag {
            if let Some(stag) = self.stag {
                data.push(((stag >> 16) & 0xFF) as u8);
                data.push(((stag >> 8) & 0xFF) as u8);
                data.push((stag & 0xFF) as u8);
            }
        }

        data
    }

    /// Unmarshals bytes into an Outer Header Creation IE
    pub fn unmarshal(payload: &[u8]) -> Result<Self, PfcpError> {
        if payload.len() < 2 {
            return Err(PfcpError::invalid_length(
                "Outer Header Creation",
                IeType::OuterHeaderCreation,
                2,
                payload.len(),
            ));
        }

        let mut offset = 0;

        // Description (2 bytes)
        let desc_value = u16::from_be_bytes([payload[offset], payload[offset + 1]]);
        let description = OuterHeaderCreationFlags::from_u16(desc_value);
        offset += 2;

        // TEID (4 bytes) - if GTP-U
        let teid = if description.gtpu_udp_ipv4 || description.gtpu_udp_ipv6 {
            if offset + 4 > payload.len() {
                return Err(PfcpError::invalid_length(
                    "Outer Header Creation TEID",
                    IeType::OuterHeaderCreation,
                    offset + 4,
                    payload.len(),
                ));
            }
            let teid_val = u32::from_be_bytes([
                payload[offset],
                payload[offset + 1],
                payload[offset + 2],
                payload[offset + 3],
            ]);
            offset += 4;
            Some(Teid(teid_val))
        } else {
            None
        };

        // IPv4 Address (4 bytes)
        let ipv4_address = if description.gtpu_udp_ipv4 || description.udp_ipv4 || description.ipv4
        {
            if offset + 4 > payload.len() {
                return Err(PfcpError::invalid_length(
                    "Outer Header Creation IPv4",
                    IeType::OuterHeaderCreation,
                    offset + 4,
                    payload.len(),
                ));
            }
            let ipv4 = Ipv4Addr::new(
                payload[offset],
                payload[offset + 1],
                payload[offset + 2],
                payload[offset + 3],
            );
            offset += 4;
            Some(ipv4)
        } else {
            None
        };

        // IPv6 Address (16 bytes)
        let ipv6_address = if description.gtpu_udp_ipv6 || description.udp_ipv6 || description.ipv6
        {
            if offset + 16 > payload.len() {
                return Err(PfcpError::invalid_length(
                    "Outer Header Creation IPv6",
                    IeType::OuterHeaderCreation,
                    offset + 16,
                    payload.len(),
                ));
            }
            let mut ipv6_bytes = [0u8; 16];
            ipv6_bytes.copy_from_slice(&payload[offset..offset + 16]);
            offset += 16;
            Some(Ipv6Addr::from(ipv6_bytes))
        } else {
            None
        };

        // Port Number (2 bytes) - for UDP
        let port_number = if description.udp_ipv4 || description.udp_ipv6 {
            if offset + 2 > payload.len() {
                return Err(PfcpError::invalid_length(
                    "Outer Header Creation port",
                    IeType::OuterHeaderCreation,
                    offset + 2,
                    payload.len(),
                ));
            }
            let port = u16::from_be_bytes([payload[offset], payload[offset + 1]]);
            offset += 2;
            Some(port)
        } else {
            None
        };

        // C-TAG (3 bytes)
        let ctag = if description.ctag {
            if offset + 3 > payload.len() {
                return Err(PfcpError::invalid_length(
                    "Outer Header Creation C-TAG",
                    IeType::OuterHeaderCreation,
                    offset + 3,
                    payload.len(),
                ));
            }
            let ctag_val = ((payload[offset] as u32) << 16)
                | ((payload[offset + 1] as u32) << 8)
                | (payload[offset + 2] as u32);
            offset += 3;
            Some(ctag_val)
        } else {
            None
        };

        // S-TAG (3 bytes)
        let stag = if description.stag {
            if offset + 3 > payload.len() {
                return Err(PfcpError::invalid_length(
                    "Outer Header Creation S-TAG",
                    IeType::OuterHeaderCreation,
                    offset + 3,
                    payload.len(),
                ));
            }
            let stag_val = ((payload[offset] as u32) << 16)
                | ((payload[offset + 1] as u32) << 8)
                | (payload[offset + 2] as u32);
            Some(stag_val)
        } else {
            None
        };

        Ok(OuterHeaderCreation {
            description,
            teid,
            ipv4_address,
            ipv6_address,
            port_number,
            ctag,
            stag,
        })
    }

    /// Wraps the Outer Header Creation in an IE
    pub fn to_ie(&self) -> Ie {
        Ie::new(IeType::OuterHeaderCreation, self.marshal())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_outer_header_creation_gtpu_ipv4() {
        let ohc = OuterHeaderCreation::gtpu_ipv4(0x12345678, "192.168.1.1".parse().unwrap());

        assert!(ohc.description.gtpu_udp_ipv4);
        assert_eq!(ohc.teid, Some(Teid(0x12345678)));
        assert_eq!(ohc.ipv4_address, Some("192.168.1.1".parse().unwrap()));

        let marshaled = ohc.marshal();
        let unmarshaled = OuterHeaderCreation::unmarshal(&marshaled).unwrap();

        assert_eq!(ohc, unmarshaled);
    }

    #[test]
    fn test_outer_header_creation_gtpu_ipv6() {
        let ohc = OuterHeaderCreation::gtpu_ipv6(0xABCDEF01, "2001:db8::1".parse().unwrap());

        assert!(ohc.description.gtpu_udp_ipv6);
        assert_eq!(ohc.teid, Some(Teid(0xABCDEF01)));
        assert_eq!(ohc.ipv6_address, Some("2001:db8::1".parse().unwrap()));

        let marshaled = ohc.marshal();
        let unmarshaled = OuterHeaderCreation::unmarshal(&marshaled).unwrap();

        assert_eq!(ohc, unmarshaled);
    }

    #[test]
    fn test_outer_header_creation_udp_ipv4() {
        let ohc = OuterHeaderCreation::udp_ipv4("10.0.0.1".parse().unwrap(), 2152);

        assert!(ohc.description.udp_ipv4);
        assert_eq!(ohc.ipv4_address, Some("10.0.0.1".parse().unwrap()));
        assert_eq!(ohc.port_number, Some(2152));
        assert_eq!(ohc.teid, None);

        let marshaled = ohc.marshal();
        let unmarshaled = OuterHeaderCreation::unmarshal(&marshaled).unwrap();

        assert_eq!(ohc, unmarshaled);
    }

    #[test]
    fn test_outer_header_creation_with_ctag() {
        let ohc = OuterHeaderCreation::gtpu_ipv4(0x1000, "192.168.1.1".parse().unwrap())
            .with_ctag(0x123456);

        assert!(ohc.description.ctag);
        assert_eq!(ohc.ctag, Some(0x123456));

        let marshaled = ohc.marshal();
        let unmarshaled = OuterHeaderCreation::unmarshal(&marshaled).unwrap();

        assert_eq!(ohc, unmarshaled);
    }

    #[test]
    fn test_outer_header_creation_to_ie() {
        let ohc = OuterHeaderCreation::gtpu_ipv4(0x12345678, "192.168.1.1".parse().unwrap());
        let ie = ohc.to_ie();

        assert_eq!(ie.ie_type, IeType::OuterHeaderCreation);

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

    #[test]
    fn test_flags_round_trip() {
        let flags = OuterHeaderCreationFlags {
            gtpu_udp_ipv4: true,
            udp_ipv6: true,
            ctag: true,
            ..OuterHeaderCreationFlags::new()
        };

        let value = flags.to_u16();
        let recovered = OuterHeaderCreationFlags::from_u16(value);

        assert_eq!(flags, recovered);
    }
}