Expand description
GTP codec — GTPv1-C/GTPv1-U (3GPP TS 29.060) and GTPv2-C (3GPP TS 29.274).
Pure: no I/O, no state, no LLM. mod.rs owns the sockets and actions.rs owns the
model’s vocabulary; everything that turns bytes into structure and back lives here so the
literal-byte tests in tests/server/gtp/codec_test.rs can pin it without a running server.
§The two rules this file exists to get right
- The GTPv1 E/S/PN flags are all-or-nothing. If any of the extension-header,
sequence-number or N-PDU-number flags is set, all four optional octets are present
(2 sequence, 1 N-PDU, 1 next-extension-header) and each field is only meaningful when
its own flag is set. Implementations routinely assume only the flagged field appears,
which desynchronises every subsequent byte. See
GtpV1Header::optional_present. - GTPv1 information elements below type 128 are fixed-length (TV), 128 and above are
variable-length (TLV) with a two-octet length. A decoder that assumes TLV throughout
misreads a Cause IE as a length prefix. See
v1_fixed_ie_len.
GTPv2-C has neither problem: its header length is decided by one flag (T) and every IE is TLIV. It is implemented here because both versions share UDP 2123 and a node has to tell them apart from the first three bits anyway.
Structs§
- Cause
Name - A cause the model can name, with its code in each version.
- Extension
Header - One GTPv1 extension header (TS 29.060 §6.1).
- GtpV1
Header - GTPv1 header (TS 29.060 §6).
- GtpV1Ie
- GtpV1
Message - A decoded GTPv1 datagram.
- GtpV2
Header - GTPv2-C header (TS 29.274 §5.1).
- GtpV2Ie
- GtpV2
Message - InnerIp
- The decoded header of the user packet a G-PDU carries.
Enums§
- Decode
Error - Encode
Error - A value the encoder refuses to put on the wire.
- GtpVersion
- Which GTP version a datagram announced in its first three bits.
Constants§
- CAUSES
- GTPC_
PORT - GTP-C signalling port (TS 29.060 §4.1, TS 29.274 §4.1). Above 1023.
- GTPU_
PORT - GTP-U user-plane port (TS 29.060 §4.1). Above 1023.
- MAX_
APN_ LABEL_ LEN - Longest DNS label an APN may carry, in octets (TS 23.003 §9.1, RFC 1035 §2.3.4).
- MAX_
GPDU_ PAYLOAD_ LEN - Largest T-PDU this server will put inside a G-PDU, in octets.
- V1_
CREATE_ PDP_ CONTEXT_ REQUEST - V1_
CREATE_ PDP_ CONTEXT_ RESPONSE - V1_
DELETE_ PDP_ CONTEXT_ REQUEST - V1_
DELETE_ PDP_ CONTEXT_ RESPONSE - V1_
ECHO_ REQUEST - V1_
ECHO_ RESPONSE - V1_
END_ MARKER - V1_
ERROR_ INDICATION - V1_
G_ PDU - V1_
IE_ ACCESS_ POINT_ NAME - V1_
IE_ CAUSE - V1_
IE_ CHARGING_ ID - V1_
IE_ END_ USER_ ADDRESS - V1_
IE_ GSN_ ADDRESS - V1_
IE_ IMEISV - V1_
IE_ IMSI - V1_
IE_ MSISDN - V1_
IE_ MS_ TIME_ ZONE - V1_
IE_ NSAPI - V1_
IE_ PROTOCOL_ CONFIG_ OPTIONS - V1_
IE_ QOS_ PROFILE - V1_
IE_ RAT_ TYPE - V1_
IE_ RECOVERY - V1_
IE_ REORDERING_ REQUIRED - V1_
IE_ SELECTION_ MODE - V1_
IE_ TEARDOWN_ IND - V1_
IE_ TEID_ CONTROL_ PLANE - V1_
IE_ TEID_ DATA_ I - V1_
IE_ USER_ LOCATION_ INFO - V1_
MAX_ SEQUENCE - Largest sequence number a GTPv1 header can carry: the field is 16 bits (TS 29.060 §6).
- V1_
SUPPORTED_ EXTENSION_ HEADERS_ NOTIFICATION - V1_
UPDATE_ PDP_ CONTEXT_ REQUEST - V1_
UPDATE_ PDP_ CONTEXT_ RESPONSE - V1_
VERSION_ NOT_ SUPPORTED - V2_
CREATE_ SESSION_ REQUEST - V2_
CREATE_ SESSION_ RESPONSE - V2_
DELETE_ SESSION_ REQUEST - V2_
DELETE_ SESSION_ RESPONSE - V2_
ECHO_ REQUEST - V2_
ECHO_ RESPONSE - V2_
IE_ APN - V2_
IE_ APN_ RESTRICTION - V2_
IE_ BEARER_ CONTEXT - V2_
IE_ CAUSE - V2_
IE_ EBI - V2_
IE_ FTEID - V2_
IE_ IMSI - V2_
IE_ MSISDN - V2_
IE_ PAA - V2_
IE_ PCO - V2_
IE_ RAT_ TYPE - V2_
IE_ RECOVERY - V2_
IE_ ULI - V2_
MAX_ SEQUENCE - Largest sequence number a GTPv2 header can carry: the field is 24 bits (TS 29.274 §5.1).
- V2_
MODIFY_ BEARER_ REQUEST - V2_
MODIFY_ BEARER_ RESPONSE
Functions§
- cause_
accepts - Does this numeric cause value grant something?
- cause_
by_ name - Look a cause up by name, tolerating the spellings a model actually produces:
"Request accepted","REQUEST_ACCEPTED","request-accepted". - cause_
names - Every cause name the model may use, for error messages and documentation.
- decode_
apn - Decode a labelled APN back to dotted form. Returns
Noneif the labels do not tile the buffer exactly, which means it was not an APN. - decode_
end_ user_ address - Decode a GTPv1 End User Address. Returns
(pdp_type, address); the address isNonefor the “give me a dynamic one” form a UE sends in a Create PDP Context Request. - decode_
fteid - Decode an F-TEID. Returns
(interface_type, teid, address). - decode_
inner_ ip - Decode the header of an encapsulated user IP packet.
Nonewhen it is not IP at all. - decode_
paa - Decode a GTPv2-C PAA. Returns
(pdn_type, address). - decode_
tbcd - Decode TBCD back to digits, stopping at the
0xFfiller. - encode_
apn - Encode an APN as DNS-style labels: one length octet per label, no trailing root label (TS 23.003 §9.1, TS 29.060 §7.7.30).
- encode_
end_ user_ address - GTPv1 End User Address (IE 128) for an IETF-allocated PDP address.
- encode_
fteid - GTPv2-C Fully Qualified TEID (IE 87), TS 29.274 §8.22.
- encode_
paa - GTPv2-C PDN Address Allocation (IE 79). PDN type 1 = IPv4, 2 = IPv6, 3 = IPv4v6.
- encode_
pco_ dns - Protocol Configuration Options carrying DNS server addresses in the network-to-MS direction (TS 24.008 §10.5.6.3; GTPv1 IE 132, GTPv2 IE 78).
- encode_
tbcd - Encode decimal digits as TBCD (telephony BCD): two digits per octet, low nibble first,
odd counts padded with the filler
0xF. Used for IMSI, MSISDN and IMEI. - encode_
v1_ ies - Serialise information elements. Fixed types emit no length; 128+ emit a two-octet length.
- encode_
v2_ grouped - Serialise IEs for use as the value of a grouped GTPv2 IE.
- find_
all_ v1 - Every IE of a type, in order.
- find_v1
- First IE of a type, if present.
- ip_
protocol_ name - parse_
v1_ ies - Parse a GTPv1-C message body into information elements.
- parse_
v2_ grouped - Parse the inner IEs of a grouped GTPv2 IE (Bearer Context and friends).
- peek_
version - The version bits of a datagram, without decoding anything else.
- rat_
type_ name - Radio access technology name (TS 29.060 §7.7.50 / TS 29.274 §8.17). The two specifications number these differently, so the version has to be passed in.
- v1_
fixed_ ie_ len - Length in octets of the value of a GTPv1 fixed-length (TV) information element.
- v1_
ie_ name - Human name for the GTPv1 IEs this server surfaces. Unknown types are reported by number.
- v1_
message_ name - Human name for a GTPv1 message type, for logs and event data.
- v2_
ie_ name - Human name for the GTPv2-C IEs this server surfaces.
- v2_
message_ name - Human name for a GTPv2-C message type.