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

Fix length calculation for GTPv2 header #3833

Merged
merged 4 commits into from Jan 29, 2023
Merged

Conversation

muelleme
Copy link
Contributor

3GPP TS 29.274 states in Section 5.5.1:
"Octets 3 to 4 represent the Message Length field. This field shall indicate the length of the message in octets excluding the mandatory part of the GTP-C header (the first 4 octets). The TEID (if present) and the Sequence Number shall be included in the length count."

Checklist:

  • If you are new to Scapy: I have checked CONTRIBUTING.md (esp. section submitting-pull-requests)
  • I squashed commits belonging together
  • I added unit tests or explained why they are not relevant
  • I executed the regression tests (using cd test && ./run_tests or tox)
  • If the PR is still not finished, please create a Draft Pull Request

@codecov
Copy link

codecov bot commented Dec 30, 2022

Codecov Report

Merging #3833 (f3f4131) into master (eba8ce4) will decrease coverage by 0.02%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master    #3833      +/-   ##
==========================================
- Coverage   49.95%   49.94%   -0.02%     
==========================================
  Files         228      228              
  Lines       53176    53176              
==========================================
- Hits        26565    26559       -6     
- Misses      26611    26617       +6     
Impacted Files Coverage Δ
scapy/contrib/gtp.py 66.27% <0.00%> (ø)
scapy/utils6.py 29.25% <0.00%> (-1.00%) ⬇️
scapy/arch/windows/__init__.py 62.21% <0.00%> (-0.57%) ⬇️
scapy/fields.py 51.72% <0.00%> (-0.11%) ⬇️
scapy/interfaces.py 73.84% <0.00%> (+1.53%) ⬆️

@@ -258,7 +258,7 @@ class GTPHeader(Packet):
def post_build(self, p, pay):
p += pay
if self.length is None:
tmp_len = len(p) - 8
tmp_len = len(p) - 4 if self.version == 2 else len(p) - 8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain how a test on the version fixes your issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely! That field is defined differently for GTPv1 Headers and GTPv2 Headers. See for instance https://en.wikipedia.org/wiki/GPRS_Tunnelling_Protocol#Header

For GTPv1:

Message Length
a 16-bit field that indicates the length of the payload in bytes (rest of the packet following the mandatory 8-byte GTP header). Includes the optional fields.

For GTPv2:

Message length
This field shall indicate the length of the message in octets excluding the mandatory of the GTP-C header (the first 4 octets).

So for GTPv1 you need to disregard 8 bytes, whereas for GTPv2 it's only 4. The GTPHeader class was correctly implementing the length calculation for GTPv1, but not for GTPv2.

I could also change the PR and overwrite this entire method in the gtp_v2.GTPHeader, which so far inherits all the logic from the gtp.GTPHeader: https://github.com/secdev/scapy/blob/master/scapy/contrib/gtp_v2.py#L242-L264
Now that I write this, I think that this is a much better place for this logic...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not necessary, thanks a lot for clarifying ! Would mind adding a very short (1-2lines) summary of this as a comment ?
Thanks !

Mike Müller added 2 commits January 18, 2023 20:24
3GPP TS 29.274 states in Section 5.5.1:
"Octets 3 to 4 represent the Message Length field. This field shall indicate
the length of the message in octets excluding the mandatory part of the GTP-C
header (the first 4 octets). The TEID (if present) and the Sequence Number
shall be included in the length count."
@gpotter2 gpotter2 merged commit 0260f97 into secdev:master Jan 29, 2023
muttiopenbts pushed a commit to muttiopenbts/scapy that referenced this pull request Feb 21, 2023
* Fix length calculation for GTPv2 header

3GPP TS 29.274 states in Section 5.5.1:
"Octets 3 to 4 represent the Message Length field. This field shall indicate
the length of the message in octets excluding the mandatory part of the GTP-C
header (the first 4 octets). The TEID (if present) and the Sequence Number
shall be included in the length count."

* Add comment on length in different GTP versions

---------

Co-authored-by: Mike Müller <mmuller@twilio.com>
@gpotter2 gpotter2 added this to the 2.6.0 milestone Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants