Skip to content

Commit

Permalink
Fqdn (#4598)
Browse files Browse the repository at this point in the history
* FQDN coding fix

* Update gtp_v2.uts

* Fix PEP8

---------

Co-authored-by: Ivan Stepanenko <[email protected]>
Co-authored-by: ElKobano <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2024
1 parent 62e335d commit 07854ab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
19 changes: 19 additions & 0 deletions scapy/contrib/gtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from scapy.layers.inet import IP, UDP
from scapy.layers.inet6 import IPv6, IP6Field
from scapy.layers.ppp import PPP
from scapy.layers.dns import DNSStrField
from scapy.packet import bind_layers, bind_bottom_up, bind_top_down, \
Packet, Raw
from scapy.volatile import RandInt, RandIP, RandNum, RandString
Expand Down Expand Up @@ -208,6 +209,24 @@ def i2m(self, pkt, val):
return ret_string


class FQDNField(DNSStrField):
"""
DNSStrField without ending null.
See ETSI TS 129.244 18.07.00 - 8.66, NOTE 1
"""

def h2i(self, pkt, x):
return bytes_encode(x)

def i2m(self, pkt, x):
return b"".join(chb(len(y)) + y for y in (k[:63] for k in x.split(b".")))

def getfield(self, pkt, s):
remain, s = super().getfield(pkt, s)
return remain, s[:-1]


TBCD_TO_ASCII = b"0123456789*#abc"


Expand Down
3 changes: 1 addition & 2 deletions scapy/contrib/gtp_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,7 @@ class IE_FQDN(gtp.IE_Base):
ShortField("length", None),
BitField("CR_flag", 0, 4),
BitField("instance", 0, 4),
ByteField("fqdn_tr_bit", 0),
StrLenField("fqdn", "", length_from=lambda x: x.length - 1)]
gtp.FQDNField("fqdn", b"", length_from=lambda x: x.length)]


class IE_NotImplementedTLV(gtp.IE_Base):
Expand Down
9 changes: 5 additions & 4 deletions test/contrib/gtp_v2.uts
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,16 @@ ie = IE_MMContext_EPS(ietype=107, length=70, CR_flag=0, instance=0, Sec_Mode=4,
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_PDNConnection, IE_FQDN, dissection
h = "d89ef3da40e2fa163e956dce08004500007f0001000040114bbd0a0a0f3d0a0f0b5b084b084b006b5a234883005f0000180f76d163006b0046008800910000020000021890aa80be385102083701a2907066f8bd9f2a28b717671c71c71c71c71c71c70100003d090002625a00028040000812345678900000000000000000006d000900880005000470677731"
h = "d89ef3da40e2fa163e956dce08004500008a0001000040114bbd0a0a0f3d0a0f0b5b084b084b00765a234883006a0000180f76d163006b0046008800910000020000021890aa80be385102083701a2907066f8bd9f2a28b717671c71c71c71c71c71c70100003d090002625a00028040000812345678900000000000000000006d0014008800100004706777310474657374056c6f63616c"
gtp = Ether(hex_bytes(h))
ie = gtp.IE_list[1].IE_list[0]
ie.fqdn_tr_bit == 4 and ie.fqdn == b'pgw1'
ie.fqdn == b'pgw1.test.local'
gtp.build().hex() == h

= IE_PDNConnection, IE_FQDN, basic instantiation
ie = IE_PDNConnection(IE_list=[IE_FQDN(ietype=136, length=5, CR_flag=0, instance=0, fqdn_tr_bit=4, fqdn=b'pgw1')], ietype=109, length=9, CR_flag=0, instance=0)
ie = IE_PDNConnection(IE_list=[IE_FQDN(ietype=136, length=5, CR_flag=0, instance=0, fqdn=b'pgw1.test.local')], ietype=109, length=9, CR_flag=0, instance=0)
ie2 = ie.IE_list[0]
ie2.fqdn_tr_bit == 4 and ie2.fqdn == b'pgw1'
ie2.fqdn == b'pgw1.test.local'

= IE_PAA, dissection
h = "3333333333332222222222228100a384080045b800ed00000000fc1193430a2a00010a2a00027f61084b00d91c47482000cd140339f4d99f66000100080002081132547600004b000800000000000001e24056000d001832f420303932f4200001e2405300030032f4205200010006570009008a000010927f0000025700090187000010927f00000247001a00196161616161616161616161616161616161616161616161616163000100014f000500017f0000034d0004000808000048000800000017000000a4105d002c0049000100e55700090385000010927f00000250001600580700000000000000000000000000000000000000007200020014005311004c"
Expand Down

0 comments on commit 07854ab

Please sign in to comment.