Skip to content
Snippets Groups Projects
Commit 8f6b9d3c authored by gpotter2's avatar gpotter2 Committed by Guillaume Valadon
Browse files

Remove duplicated fields

parent 3185c101
No related branches found
No related tags found
No related merge requests found
...@@ -41,6 +41,8 @@ GTPmessageType = { 1: "echo_request", ...@@ -41,6 +41,8 @@ GTPmessageType = { 1: "echo_request",
21: "delete_pdp_context_res", 21: "delete_pdp_context_res",
26: "error_indication", 26: "error_indication",
27: "pdu_notification_req", 27: "pdu_notification_req",
31: "supported_extension_headers_notification",
254: "end_marker",
255: "g_pdu" } 255: "g_pdu" }
IEType = { 1: "Cause", IEType = { 1: "Cause",
...@@ -177,20 +179,27 @@ class TBCDByteField(StrFixedLenField): ...@@ -177,20 +179,27 @@ class TBCDByteField(StrFixedLenField):
TBCD_TO_ASCII = "0123456789*#abc" TBCD_TO_ASCII = "0123456789*#abc"
class GTP_UDPPort_ExtensionHeader(Packet): class GTP_ExtensionHeader(Packet):
fields_desc=[ ShortField("length", 0x40), @classmethod
BitField("udp_port", None, 48), def dispatch_hook(cls, _pkt=None, *args, **kargs):
if _pkt == None:
return GTP_UDPPort_ExtensionHeader
return cls
class GTP_UDPPort_ExtensionHeader(GTP_ExtensionHeader):
fields_desc=[ ByteField("length", 0x40),
ShortField("udp_port", None),
ByteEnumField("next_ex", 0, ExtensionHeadersTypes), ] ByteEnumField("next_ex", 0, ExtensionHeadersTypes), ]
class GTP_PDCP_PDU_ExtensionHeader(Packet): class GTP_PDCP_PDU_ExtensionHeader(GTP_ExtensionHeader):
fields_desc=[ ShortField("length", 0x01), fields_desc=[ ByteField("length", 0x01),
ByteField("pdcp_pdu", None), ShortField("pdcp_pdu", None),
ByteField("pdcp_pdu", None),
ByteEnumField("next_ex", 0, ExtensionHeadersTypes), ] ByteEnumField("next_ex", 0, ExtensionHeadersTypes), ]
class GTPHeader(Packet): class GTPHeader(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP Header" name = "GTP-C Header"
fields_desc=[ BitField("version", 1, 3), fields_desc=[ BitField("version", 1, 3),
BitField("PT", 1, 1), BitField("PT", 1, 1),
BitField("reserved", 0, 1), BitField("reserved", 0, 1),
...@@ -221,17 +230,39 @@ class GTPHeader(Packet): ...@@ -221,17 +230,39 @@ class GTPHeader(Packet):
@classmethod @classmethod
def dispatch_hook(cls, _pkt=None, *args, **kargs): def dispatch_hook(cls, _pkt=None, *args, **kargs):
if _pkt and len(_pkt) >= 1:
if (struct.unpack("B", _pkt[0])[0] >> 5) & 0x7 == 2:
import gtp_v2
return gtp_v2.GTPHeader
if _pkt and len(_pkt) >= 8: if _pkt and len(_pkt) >= 8:
if struct.unpack("!B", _pkt[1:2])[0] == 255: _gtp_type = struct.unpack("!B", _pkt[1:2])[0]
return GTP_U_Header return GTPforcedTypes.get(_gtp_type, GTPHeader)
return cls return cls
class GTP_U_Header(GTPHeader):
# 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP-U Header"
# GTP-U protocol is used to transmit T-PDUs between GSN pairs (or between an SGSN and an RNC in UMTS),
# encapsulated in G-PDUs. A G-PDU is a packet including a GTP-U header and a T-PDU. The Path Protocol
# defines the path and the GTP-U header defines the tunnel. Several tunnels may be multiplexed on a single path.
# Some gtp_types have to be associated with a certain type of header
GTPforcedTypes = {
16: GTPHeader,
17: GTPHeader,
18: GTPHeader,
19: GTPHeader,
20: GTPHeader,
21: GTPHeader,
26: GTP_U_Header,
27: GTPHeader,
254: GTP_U_Header,
255: GTP_U_Header
}
class GTPEchoRequest(Packet): class GTPEchoRequest(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP Echo Request" name = "GTP Echo Request"
fields_desc = [ XBitField("seq", 0, 16),
ByteField("npdu", 0),
ByteEnumField("next_ex", 0, ExtensionHeadersTypes),]
def hashret(self): def hashret(self):
return struct.pack("H", self.seq) return struct.pack("H", self.seq)
...@@ -658,6 +689,11 @@ class IE_PrivateExtension(IE_Base): ...@@ -658,6 +689,11 @@ class IE_PrivateExtension(IE_Base):
StrLenField("extention_value", "", StrLenField("extention_value", "",
length_from=lambda x: x.length)] length_from=lambda x: x.length)]
class IE_ExtensionHeaderList(IE_Base):
name = "Extension Header List"
fields_desc = [ByteEnumField("ietype", 141, IEType),
FieldLenField("length", None, length_of="extension_headers"),
FieldListField("extension_headers", [64, 192], ByteField("", 0))]
class IE_NotImplementedTLV(Packet): class IE_NotImplementedTLV(Packet):
name = "IE not implemented" name = "IE not implemented"
...@@ -688,6 +724,7 @@ ietypecls = {1: IE_Cause, ...@@ -688,6 +724,7 @@ ietypecls = {1: IE_Cause,
133: IE_GSNAddress, 133: IE_GSNAddress,
134: IE_MSInternationalNumber, 134: IE_MSInternationalNumber,
135: IE_QoS, 135: IE_QoS,
141: IE_ExtensionHeaderList,
148: IE_CommonFlags, 148: IE_CommonFlags,
149: IE_APNRestriction, 149: IE_APNRestriction,
151: IE_RATType, 151: IE_RATType,
...@@ -718,10 +755,7 @@ def IE_Dispatcher(s): ...@@ -718,10 +755,7 @@ def IE_Dispatcher(s):
class GTPEchoResponse(Packet): class GTPEchoResponse(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP Echo Response" name = "GTP Echo Response"
fields_desc = [ XBitField("seq", 0, 16), fields_desc = [ PacketListField("IE_list", [], IE_Dispatcher) ]
ByteField("npdu", 0),
ByteEnumField("next_ex", 0, ExtensionHeadersTypes),
PacketListField("IE_list", [], IE_Dispatcher) ]
def hashret(self): def hashret(self):
return struct.pack("H", self.seq) return struct.pack("H", self.seq)
...@@ -733,10 +767,7 @@ class GTPEchoResponse(Packet): ...@@ -733,10 +767,7 @@ class GTPEchoResponse(Packet):
class GTPCreatePDPContextRequest(Packet): class GTPCreatePDPContextRequest(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP Create PDP Context Request" name = "GTP Create PDP Context Request"
fields_desc = [ ShortField("seq", RandShort()), fields_desc = [ PacketListField("IE_list", [ IE_TEIDI(), IE_NSAPI(), IE_GSNAddress(),
ByteField("npdu", 0),
ByteEnumField("next_ex", 0, ExtensionHeadersTypes),
PacketListField("IE_list", [ IE_TEIDI(), IE_NSAPI(), IE_GSNAddress(),
IE_GSNAddress(), IE_GSNAddress(),
IE_NotImplementedTLV(ietype=135, length=15,data=RandString(15)) ], IE_NotImplementedTLV(ietype=135, length=15,data=RandString(15)) ],
IE_Dispatcher) ] IE_Dispatcher) ]
...@@ -746,10 +777,7 @@ class GTPCreatePDPContextRequest(Packet): ...@@ -746,10 +777,7 @@ class GTPCreatePDPContextRequest(Packet):
class GTPCreatePDPContextResponse(Packet): class GTPCreatePDPContextResponse(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP Create PDP Context Response" name = "GTP Create PDP Context Response"
fields_desc = [ ShortField("seq", RandShort()), fields_desc = [ PacketListField("IE_list", [], IE_Dispatcher) ]
ByteField("npdu", 0),
ByteEnumField("next_ex", 0, ExtensionHeadersTypes),
PacketListField("IE_list", [], IE_Dispatcher) ]
def hashret(self): def hashret(self):
return struct.pack("H", self.seq) return struct.pack("H", self.seq)
...@@ -761,10 +789,7 @@ class GTPCreatePDPContextResponse(Packet): ...@@ -761,10 +789,7 @@ class GTPCreatePDPContextResponse(Packet):
class GTPUpdatePDPContextRequest(Packet): class GTPUpdatePDPContextRequest(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP Update PDP Context Request" name = "GTP Update PDP Context Request"
fields_desc = [ShortField("seq", RandShort()), fields_desc = [PacketListField("IE_list", [
ByteField("npdu", 0),
ByteEnumField("next_ex", 0, ExtensionHeadersTypes),
PacketListField("IE_list", [
IE_Cause(), IE_Cause(),
IE_Recovery(), IE_Recovery(),
IE_TEIDI(), IE_TEIDI(),
...@@ -793,10 +818,7 @@ class GTPUpdatePDPContextRequest(Packet): ...@@ -793,10 +818,7 @@ class GTPUpdatePDPContextRequest(Packet):
class GTPUpdatePDPContextResponse(Packet): class GTPUpdatePDPContextResponse(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP Update PDP Context Response" name = "GTP Update PDP Context Response"
fields_desc = [ShortField("seq", RandShort()), fields_desc = [PacketListField("IE_list", None, IE_Dispatcher)]
ByteField("npdu", 0),
ByteEnumField("next_ex", 0, ExtensionHeadersTypes),
PacketListField("IE_list", None, IE_Dispatcher)]
def hashret(self): def hashret(self):
return struct.pack("H", self.seq) return struct.pack("H", self.seq)
...@@ -805,49 +827,37 @@ class GTPUpdatePDPContextResponse(Packet): ...@@ -805,49 +827,37 @@ class GTPUpdatePDPContextResponse(Packet):
class GTPErrorIndication(Packet): class GTPErrorIndication(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP Error Indication" name = "GTP Error Indication"
fields_desc = [ XBitField("seq", 0, 16), fields_desc = [ PacketListField("IE_list", [], IE_Dispatcher) ]
ByteField("npdu", 0),
ByteField("next_ex",0),
PacketListField("IE_list", [], IE_Dispatcher) ]
class GTPDeletePDPContextRequest(Packet): class GTPDeletePDPContextRequest(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP Delete PDP Context Request" name = "GTP Delete PDP Context Request"
fields_desc = [ XBitField("seq", 0, 16), fields_desc = [ PacketListField("IE_list", [], IE_Dispatcher) ]
ByteField("npdu", 0),
ByteEnumField("next_ex", 0, ExtensionHeadersTypes),
PacketListField("IE_list", [], IE_Dispatcher) ]
class GTPDeletePDPContextResponse(Packet): class GTPDeletePDPContextResponse(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP Delete PDP Context Response" name = "GTP Delete PDP Context Response"
fields_desc = [ XBitField("seq", 0, 16), fields_desc = [ PacketListField("IE_list", [], IE_Dispatcher) ]
ByteField("npdu", 0),
ByteField("next_ex",0),
PacketListField("IE_list", [], IE_Dispatcher) ]
class GTPPDUNotificationRequest(Packet): class GTPPDUNotificationRequest(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP PDU Notification Request" name = "GTP PDU Notification Request"
fields_desc = [ XBitField("seq", 0, 16), fields_desc = [ PacketListField("IE_list", [ IE_IMSI(),
ByteField("npdu", 0),
ByteEnumField("next_ex", 0, ExtensionHeadersTypes),
PacketListField("IE_list", [ IE_IMSI(),
IE_TEICP(TEICI=RandInt()), IE_TEICP(TEICI=RandInt()),
IE_EndUserAddress(PDPTypeNumber=0x21), IE_EndUserAddress(PDPTypeNumber=0x21),
IE_AccessPointName(), IE_AccessPointName(),
IE_GSNAddress(address="127.0.0.1"), IE_GSNAddress(address="127.0.0.1"),
], IE_Dispatcher) ] ], IE_Dispatcher) ]
class GTP_U_Header(GTPHeader): class GTPSupportedExtensionHeadersNotification(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) name = "GTP Supported Extension Headers Notification"
name = "GTP-U Header" fields_desc = [ PacketListField("IE_list", [ IE_ExtensionHeaderList(),
# GTP-U protocol is used to transmit T-PDUs between GSN pairs (or between an SGSN and an RNC in UMTS), ], IE_Dispatcher) ]
# encapsulated in G-PDUs. A G-PDU is a packet including a GTP-U header and a T-PDU. The Path Protocol
# defines the path and the GTP-U header defines the tunnel. Several tunnels may be multiplexed on a single path.
pass
class GTPErrorIndication(Packet):
name = "GTP Error Indication"
fields_desc = [ PacketListField("IE_list", [], IE_Dispatcher) ]
class GTPmorethan1500(Packet): class GTPmorethan1500(Packet):
# 3GPP TS 29.060 V9.1.0 (2009-12) # 3GPP TS 29.060 V9.1.0 (2009-12)
name = "GTP More than 1500" name = "GTP More than 1500"
...@@ -857,8 +867,8 @@ class GTPmorethan1500(Packet): ...@@ -857,8 +867,8 @@ class GTPmorethan1500(Packet):
# Bind GTP-C # Bind GTP-C
bind_layers(UDP, GTPHeader, dport = 2123) bind_layers(UDP, GTPHeader, dport = 2123)
bind_layers(UDP, GTPHeader, sport = 2123) bind_layers(UDP, GTPHeader, sport = 2123)
bind_layers(GTPHeader, GTPEchoRequest, gtp_type=1) bind_layers(GTPHeader, GTPEchoRequest, gtp_type=1, S=1)
bind_layers(GTPHeader, GTPEchoResponse, gtp_type=2) bind_layers(GTPHeader, GTPEchoResponse, gtp_type=2, S=1)
bind_layers(GTPHeader, GTPCreatePDPContextRequest, gtp_type=16) bind_layers(GTPHeader, GTPCreatePDPContextRequest, gtp_type=16)
bind_layers(GTPHeader, GTPCreatePDPContextResponse, gtp_type=17) bind_layers(GTPHeader, GTPCreatePDPContextResponse, gtp_type=17)
bind_layers(GTPHeader, GTPUpdatePDPContextRequest, gtp_type=18) bind_layers(GTPHeader, GTPUpdatePDPContextRequest, gtp_type=18)
...@@ -866,12 +876,14 @@ bind_layers(GTPHeader, GTPUpdatePDPContextResponse, gtp_type=19) ...@@ -866,12 +876,14 @@ bind_layers(GTPHeader, GTPUpdatePDPContextResponse, gtp_type=19)
bind_layers(GTPHeader, GTPDeletePDPContextRequest, gtp_type=20) bind_layers(GTPHeader, GTPDeletePDPContextRequest, gtp_type=20)
bind_layers(GTPHeader, GTPDeletePDPContextResponse, gtp_type=21) bind_layers(GTPHeader, GTPDeletePDPContextResponse, gtp_type=21)
bind_layers(GTPHeader, GTPPDUNotificationRequest, gtp_type=27) bind_layers(GTPHeader, GTPPDUNotificationRequest, gtp_type=27)
bind_layers(GTPHeader, GTP_UDPPort_ExtensionHeader, next_ex = 64, E = 1) bind_layers(GTPHeader, GTPSupportedExtensionHeadersNotification, gtp_type=31, S=1)
bind_layers(GTPHeader, GTP_PDCP_PDU_ExtensionHeader, next_ex = 192, E = 1) bind_layers(GTPHeader, GTP_UDPPort_ExtensionHeader, next_ex=64, E=1)
bind_layers(GTPHeader, GTP_PDCP_PDU_ExtensionHeader, next_ex=192, E=1)
# Bind GTP-U # Bind GTP-U
bind_layers(UDP, GTP_U_Header, dport = 2152) bind_layers(UDP, GTP_U_Header, dport = 2152)
bind_layers(UDP, GTP_U_Header, sport = 2152) bind_layers(UDP, GTP_U_Header, sport = 2152)
bind_layers(GTP_U_Header, GTPErrorIndication, gtp_type=26, S=1)
bind_layers(GTP_U_Header, IP, gtp_type = 255) bind_layers(GTP_U_Header, IP, gtp_type = 255)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -11,6 +11,11 @@ a = GTPHeader() ...@@ -11,6 +11,11 @@ a = GTPHeader()
assert a.version == 1 assert a.version == 1
assert a.E == a.S == a.PN == 0 assert a.E == a.S == a.PN == 0
= GTP_U_Header detection
a = GTPHeader(str(GTP_U_Header()/GTPErrorIndication()))
assert isinstance(a, GTP_U_Header)
= GTPCreatePDPContextRequest(), basic instanciation = GTPCreatePDPContextRequest(), basic instanciation
gtp = IP()/UDP(dport=2123)/GTPHeader(teid=2807)/GTPCreatePDPContextRequest() gtp = IP()/UDP(dport=2123)/GTPHeader(teid=2807)/GTPCreatePDPContextRequest()
gtp.dport == 2123 and gtp.teid == 2807 and len(gtp.IE_list) == 5 gtp.dport == 2123 and gtp.teid == 2807 and len(gtp.IE_list) == 5
...@@ -294,4 +299,4 @@ ie.ietype == 251 and ie.ipv4_address == '127.0.0.1' and ie.ipv6_address == '::1' ...@@ -294,4 +299,4 @@ ie.ietype == 251 and ie.ipv4_address == '127.0.0.1' and ie.ipv6_address == '::1'
= IE_PrivateExtension(), basic instantiation = IE_PrivateExtension(), basic instantiation
ie = IE_PrivateExtension(extention_value='hello') ie = IE_PrivateExtension(extention_value='hello')
ie.ietype == 255 and ie.extention_value == 'hello' ie.ietype == 255 and ie.extention_value == 'hello'
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment