From 75a8ca88e7c92f9ccdbf7cb6f32f2971c6af1930 Mon Sep 17 00:00:00 2001 From: pro-laxmi Date: Sat, 14 Nov 2026 22:45:08 +0530 Subject: [PATCH] gtp_v2: allow building IE_Indication, IE_MMContext_EPS, and PCO_S_Nssai without explicit length --- scapy/contrib/gtp_v2.py | 18 +++++++++++------- test/contrib/gtp_v2.uts | 12 ++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/scapy/contrib/gtp_v2.py b/scapy/contrib/gtp_v2.py index a546a31ba28..83260865212 100755 --- a/scapy/contrib/gtp_v2.py +++ b/scapy/contrib/gtp_v2.py @@ -573,8 +573,8 @@ class IE_MMContext_EPS(gtp.IE_Base): ThreeBytesField("Nas_ul_count", 0), BitField("Kasme", 0, 256), ConditionalField(StrLenField("fields", "", - length_from=lambda x: x.length - 41), - lambda pkt: pkt.length > 40)] + length_from=lambda x: (x.length - 41) if x.length is not None else 0), + lambda pkt: pkt.length > 40 if pkt.length is not None else False)] class IE_PDNConnection(gtp.IE_Base): @@ -779,7 +779,7 @@ class IE_MSISDN(gtp.IE_Base): class IE_Indication(gtp.IE_Base): name = "IE Indication" fields_desc = [ByteEnumField("ietype", 77, IEType), - ShortField("length", None), + ShortField("length", 3), BitField("CR_flag", 0, 4), BitField("instance", 0, 4), ConditionalField( @@ -1151,13 +1151,17 @@ class PCO_S_Nssai(PCO_Protocol): fields_desc = [ShortEnumField("type", None, PCO_PROTOCOL_TYPES), ByteField("length", None), ConditionalField( - ByteField("SST", 0), lambda pkt: pkt.length > 0), + ByteField("SST", 0), + lambda pkt: pkt.length > 0 if pkt.length is not None else True), ConditionalField( - ShortField("SD", 0), lambda pkt: pkt.length > 1), + ShortField("SD", 0), + lambda pkt: pkt.length > 1 if pkt.length is not None else False), ConditionalField( - ByteField("Hplmn_Sst", 0), lambda pkt: pkt.length >= 4), + ByteField("Hplmn_Sst", 0), + lambda pkt: pkt.length >= 4 if pkt.length is not None else False), ConditionalField( - ShortField("Hplmn_Sd", 0), lambda pkt: pkt.length > 4)] + ShortField("Hplmn_Sd", 0), + lambda pkt: pkt.length > 4 if pkt.length is not None else False)] class PCO_Qos_Rules(PCO_Protocol): diff --git a/test/contrib/gtp_v2.uts b/test/contrib/gtp_v2.uts index 6a84e036077..7693c3d4f57 100644 --- a/test/contrib/gtp_v2.uts +++ b/test/contrib/gtp_v2.uts @@ -170,6 +170,12 @@ ie.ietype == 77 and ie.PS == 1 and ie.CPRAI == 1 ie = IE_Indication(ietype='Indication', length=8, DTF=1, PPSI=1) ie.ietype == 77 and ie.DTF == 1 and ie.PPSI == 1 += IE_Indication, build without length +ie = IE_Indication(PS=1) +assert bytes(ie) == b'M\x00\x03\x00\x00\x08\x00' +d = IE_Indication(bytes(ie)) +assert d.PS == 1 and d.length == 3 + = IE_PCO, dissection h = "333333333333222222222222810083840800458800a500000000fd1183bb0a2a00010a2a0002084b76a00091cf0b48210085bd574af24c68e300020002001000570009008b000010927f0000025700090187000010927f0000024f000500017f0000037f000100004e00220080000d040a2a0003000d040a2a00038021100300001081060a2a000483060a2a00045d00250049000100660200020010005700090081000010927f0000025700090285000010927f000002dd9f22c6" gtp = Ether(hex_bytes(h)) @@ -211,6 +217,12 @@ ie.Sec_Mode == 4 and ie.Nhi == 0 and ie.Drxi == 1 and ie.Ksi == 0 and ie.Num_qui ie = IE_MMContext_EPS(ietype=107, length=70, CR_flag=0, instance=0, Sec_Mode=4, Nhi=0, Drxi=1, Ksi=0, Num_quint=0, Num_Quad=0, Uambri=0, Osci=0, Sambri=1, Nas_algo=1, Nas_cipher=1, Nas_dl_count=2, Nas_ul_count=2, Kasme=11111111111111111111111111111111111111111111111111111111111111111111111111111) ie.Sec_Mode == 4 and ie.Nhi == 0 and ie.Drxi == 1 and ie.Ksi == 0 and ie.Num_quint == 0 and ie.Num_Quad == 0 and ie.Uambri == 0 and ie.Osci == 0 and ie.Sambri == 1 and ie.Nas_algo == 1 and ie.Nas_cipher == 1 and ie.Nas_dl_count == 2 and ie.Nas_ul_count == 2 and ie.Kasme == 11111111111111111111111111111111111111111111111111111111111111111111111111111 += IE_MMContext_EPS, build without length +ie = IE_MMContext_EPS() +assert len(bytes(ie)) == 45 +d = IE_MMContext_EPS(bytes(ie)) +assert d.length == 41 + = IE_PDNConnection, IE_FQDN, dissection h = "d89ef3da40e2fa163e956dce08004500008a0001000040114bbd0a0a0f3d0a0f0b5b084b084b00765a234883006a0000180f76d163006b0046008800910000020000021890aa80be385102083701a2907066f8bd9f2a28b717671c71c71c71c71c71c70100003d090002625a00028040000812345678900000000000000000006d0014008800100004706777310474657374056c6f63616c" gtp = Ether(hex_bytes(h))