pycrate-org / pycrate

A Python library to ease the development of encoders and decoders for various protocols and file formats, especially telecom ones. Provides an ASN.1 compiler and a CSN.1 runtime.
https://github.com/pycrate-org/pycrate
GNU Lesser General Public License v2.1
39 stars 9 forks source link

GTP-C "Inst" field is ignored and set to 0 #11

Closed x0ul closed 3 months ago

x0ul commented 3 months ago

First, thanks for writing pycrate, it's an amazing feat of work and has been really helpful!

I am using the pycrate_mobile.TS29247 library for GTPC. While working on the "bearer context to be created" IE, I noticed that when I supply an instance value of 3 to an F-TEID (representing the F-TEID for the PGW on S5/S8-U) and inspect the resulting message, I see that the instance value is actually set to 0, not 3 as expected. Here's an example:

In [10]: import pycrate_mobile.TS29274_GTPC as pycrate_gtpc

In [11]: v=[{'Hdr': {'Type': 73, 'Inst': 0}, 'Data': {'Val': 5}}, {'Hdr': {'Type': 80, 'Inst': 0}, 'Data': {'PCI': True, 'PL': 8, 'PVI': False, 'QCI': 9, 'MaxBitRateUL': 1, 'MaxBitRateDL': 2, 'GuaranteedBitRateU
    ...: L': 3, 'GuaranteedBitRateDL': 4}}, {'Hdr': {'Type': 87, 'Inst': 0}, 'Data': {'V4': 1, 'V6': 0, 'IF': 0, 'TEID_GREKey': 305419896, 'IPv4Addr': b'\x01\x02\x03\x04'}}, {'Hdr': {'Type': 87, 'Inst': 3}, 'Dat
    ...: a': {'V4': 1, 'V6': 0, 'IF': 5, 'TEID_GREKey': 2596069104, 'IPv4Addr': b'\x05\x06\x07\x08'}}]

In [12]: pycrate_gtpc.CreateSessionReq_BearerContexttobecreated(val=v)
Out[12]: <CreateSessionReq_BearerContexttobecreated : <EPSBearerID : <Hdr : <Type : 73 (EPS Bearer ID (EBI))><Len : 1><spare : 0><Inst : 0><TypeExt [transparent] : 0>><Data : <spare : 0><Val : 5><ext : 0x>>><BearerLevelQoS : <Hdr : <Type : 80 (Bearer Level Quality of Service (Bearer QoS))><Len : 22><spare : 0><Inst : 0><TypeExt [transparent] : 0>><Data : <spare : 0><PCI : True><PL : 8><spare : 0><PVI : False><QCI : 9><MaxBitRateUL : 1><MaxBitRateDL : 2><GuaranteedBitRateUL : 3><GuaranteedBitRateDL : 4><ext : 0x>>><S1UeNodeBFTEID : <Hdr : <Type : 87 (Fully Qualified Tunnel Endpoint Identifier (F-TEID))><Len : 9><spare : 0><Inst : 0><TypeExt [transparent] : 0>><Data : <V4 : 1><V6 : 0><IF : 0 (S1-U eNodeB GTP-U interface)><TEID_GREKey : 0x12345678><IPv4Addr : 1.2.3.4><IPv6Addr [transparent] : b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'><ext : 0x>>><S1UeNodeBFTEID : <Hdr : <Type : 87 (Fully Qualified Tunnel Endpoint Identifier (F-TEID))><Len : 9><spare : 0><Inst : 0><TypeExt [transparent] : 0>><Data : <V4 : 1><V6 : 0><IF : 5 (S5/S8 PGW GTP-U interface)><TEID_GREKey : 0x9abcdef0><IPv4Addr : 5.6.7.8><IPv6Addr [transparent] : b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'><ext : 0x>>>>

Specifically, note the last IE above is S1UeNodeBFTEID : <Hdr : <Type : 87 (Fully Qualified Tunnel Endpoint Identifier (F-TEID))><Len : 9><spare : 0><Inst : 0>... rather than having instance value 3.

I can work around the issue by setting the ie["Hdr"]["Inst"] = 3 after initializing the pycrate object, but it's not ideal.

Thanks again!

mitshell commented 3 months ago

Thanks for reporting the issue. https://github.com/pycrate-org/pycrate/commit/0c9e6c78d74ef7354ee8ebdbd2d727064e76d8e0 should solve it. GTP-C IE header fields were not properly set, besides of their Type and TypeExt before. The commit fixes this.

x0ul commented 3 months ago

Much appreciated! Yes, that appears to have fixed the issue. Setting the "Inst" field works as expected without any workarounds. Thank you!