Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GTPv2 layer #1634

Merged
merged 11 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
823 changes: 820 additions & 3 deletions Packet++/header/GtpLayer.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Packet++/header/LdapLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace pcpp
// cppcheck-suppress noExplicitConstructor
/**
* Construct LdapOperationType from Value enum
* @param[in] value the opetation type enum value
* @param[in] value the operation type enum value
*/
constexpr LdapOperationType(Value value) : m_Value(value)
{}
Expand Down
9 changes: 7 additions & 2 deletions Packet++/header/ProtocolType.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ namespace pcpp
const ProtocolType GTPv1 = 32;

/**
* GTP protocol family (currently only GTPv1)
* GTP protocol family (GTPv1 and GTPv2)
*/
const ProtocolTypeFamily GTP = 0x20;
const ProtocolTypeFamily GTP = 0x2039;

/**
* IEEE 802.3 Ethernet protocol
Expand Down Expand Up @@ -352,6 +352,11 @@ namespace pcpp
*/
const ProtocolType WireGuard = 56;

/**
* GTPv2 protocol
*/
const ProtocolType GTPv2 = 57;

/**
* An enum representing OSI model layers
*/
Expand Down
12 changes: 10 additions & 2 deletions Packet++/header/TLVData.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace pcpp
{
protected:
/** A struct representing the TLV construct */
#pragma pack(push, 1)
struct TLVRawData
{
/** Record type */
Expand All @@ -32,6 +33,7 @@ namespace pcpp
/** Record value (variable size) */
uint8_t recordValue[];
};
#pragma pack(pop)

TLVRawData* m_Data;

Expand Down Expand Up @@ -196,7 +198,7 @@ namespace pcpp
return 0;

T result;
memcpy(&result, m_Data->recordValue + offset, sizeof(T));
memcpy(&result, m_Data->recordValue + getValueOffset() + offset, sizeof(T));
return result;
}

Expand All @@ -214,7 +216,7 @@ namespace pcpp
if (getDataSize() < sizeof(T))
return false;

memcpy(m_Data->recordValue + valueOffset, &newValue, sizeof(T));
memcpy(m_Data->recordValue + getValueOffset() + valueOffset, &newValue, sizeof(T));
return true;
}

Expand All @@ -227,6 +229,12 @@ namespace pcpp
* @return The size of the record value (meaning the size of the 'V' part in TLV)
*/
virtual size_t getDataSize() const = 0;

protected:
virtual size_t getValueOffset() const
{
return 0;
}
};

/**
Expand Down
718 changes: 704 additions & 14 deletions Packet++/src/GtpLayer.cpp

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Packet++/src/TcpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "SomeIpLayer.h"
#include "SmtpLayer.h"
#include "LdapLayer.h"
#include "GtpLayer.h"
#include "PacketUtils.h"
#include "Logger.h"
#include "DeprecationUtils.h"
Expand Down Expand Up @@ -418,6 +419,9 @@ namespace pcpp
if (!m_NextLayer)
m_NextLayer = new PayloadLayer(payload, payloadLen, this, m_Packet);
}
else if ((GtpV2Layer::isGTPv2Port(portDst) || GtpV2Layer::isGTPv2Port(portSrc)) &&
GtpV2Layer::isDataValid(payload, payloadLen))
m_NextLayer = new GtpV2Layer(payload, payloadLen, this, m_Packet);
else
m_NextLayer = new PayloadLayer(payload, payloadLen, this, m_Packet);
}
Expand Down
3 changes: 3 additions & 0 deletions Packet++/src/UdpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ namespace pcpp
else if ((GtpV1Layer::isGTPv1Port(portDst) || GtpV1Layer::isGTPv1Port(portSrc)) &&
GtpV1Layer::isGTPv1(udpData, udpDataLen))
m_NextLayer = new GtpV1Layer(udpData, udpDataLen, this, m_Packet);
else if ((GtpV2Layer::isGTPv2Port(portDst) || GtpV2Layer::isGTPv2Port(portSrc)) &&
GtpV2Layer::isDataValid(udpData, udpDataLen))
m_NextLayer = new GtpV2Layer(udpData, udpDataLen, this, m_Packet);
else if ((DhcpV6Layer::isDhcpV6Port(portSrc) || DhcpV6Layer::isDhcpV6Port(portDst)) &&
(DhcpV6Layer::isDataValid(udpData, udpDataLen)))
m_NextLayer = new DhcpV6Layer(udpData, udpDataLen, this, m_Packet);
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ PcapPlusPlus currently supports parsing, editing and creation of packets of the
### Transport Layer (L4)

26. COTP
27. GTP (v1)
27. GTP (v1 & v2)
28. IPSec AH & ESP - parsing only (no editing capabilities)
29. TCP
30. TPKT
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
40b4f0d8d93c288a1ccb07d9080045680028ccbe00003d117b5e6f47ec31ddb1fc15084b084b0014273b4c22002ad37d15901a4a4310
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
40b4f0d8d93c288a1ccb07d9080045680039ccbe00003d117b4d6f47ec31ddb1fc15084b084b002509a94c220019d37d15901a4a431056000d7c1864f6292e1864f62901ce6621
1 change: 1 addition & 0 deletions Tests/Packet++Test/PacketExamples/gtpv2-over-tcp.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
08b4b11a46ad105badb0f507080045000039000100004006f641c0a80164c0a801c8084b084b000000000000000050022000646000004825000900000000003039000300010011
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
08b4b11a46ad105badb0f50708004500002b000100004011f644c0a80164c0a801c8084b084b00179a41500100090030390003000100113132
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
08b4b11a46ad105badb0f507080045000041000100004011f62ec0a80164c0a801c8084b084b002d92d3500100090030390003000100114c21001400015666010932900100080033879334495183f6
1 change: 1 addition & 0 deletions Tests/Packet++Test/PacketExamples/gtpv2-with-teid.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
40b4f0d8d93c288a1ccb07d908004568008bccbe00003d117afb6f47ec31ddb1fc15084b084b007700004822006bd37d15901a4a430056000d001864f6292e1864f62901ce66215300030064f62952000100065700090086a43ed0306f47ec314800080000000800000008004b00080053028970726123607200020023005d00120049000100055700090184a430f3e26f47ec430300010012
Binary file added Tests/Packet++Test/PacketExamples/gtpv2.pcap
Binary file not shown.
9 changes: 6 additions & 3 deletions Tests/Packet++Test/TestDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ PTF_TEST_CASE(RadiusLayerCreationTest);
PTF_TEST_CASE(RadiusLayerEditTest);

// Implemented in GtpTests.cpp
PTF_TEST_CASE(GtpLayerParsingTest);
PTF_TEST_CASE(GtpLayerCreationTest);
PTF_TEST_CASE(GtpLayerEditTest);
PTF_TEST_CASE(GtpV1LayerParsingTest);
PTF_TEST_CASE(GtpV1LayerCreationTest);
PTF_TEST_CASE(GtpV1LayerEditTest);
PTF_TEST_CASE(GtpV2LayerParsingTest);
PTF_TEST_CASE(GtpV2LayerCreationTest);
PTF_TEST_CASE(GtpV2LayerEditTest);

// Implemented in BgpTests.cpp
PTF_TEST_CASE(BgpLayerParsingTest);
Expand Down
Loading
Loading