Skip to content

Commit fa62b89

Browse files
authored
fix(l2tp): L2TPv2.from_data drops the L flag and the Length field (#1174)
`_make_data` returned the parsed Length under `length`, which `ProtocolBase.__init__` consumes as the parse length, so it never reached `make()` and the rebuild cleared `L` and omitted the Length octets. `make()` now takes it as `total_length`, as #1166 did for IPv4 and UDP, and `_make_data` passes it under that key. `test_link_unit` called `make(length=20)` directly and is updated to the new keyword. All 24 T/L/S/O(+pad) combinations now round-trip. Closes #1172
1 parent 8565b74 commit fa62b89

3 files changed

Lines changed: 118 additions & 6 deletions

File tree

‎pcapkit/protocols/link/l2tpv2.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def make(self,
245245
type_namespace: 'Optional[dict[str, int] | dict[int, str] | Type[StdlibEnum] | Type[AenumEnum]]' = None, # pylint: disable=line-too-long
246246
type_reversed: 'bool' = False,
247247
priority: 'bool' = False,
248-
length: 'Optional[int]' = None,
248+
total_length: 'Optional[int]' = None,
249249
tunnel_id: 'int' = 0,
250250
session_id: 'int' = 0,
251251
ns: 'Optional[int]' = None,
@@ -262,7 +262,8 @@ def make(self,
262262
type_namespace: Namespace of type.
263263
type_reversed: Reversed namespace of type.
264264
priority: Priority flag.
265-
length: Length of packet data.
265+
total_length: Length of the datagram, header included; the Length
266+
field and its ``L`` flag are omitted when this is :data:`None`.
266267
tunnel_id: Tunnel ID.
267268
session_id: Session ID.
268269
ns: Sequence number.
@@ -281,13 +282,13 @@ def make(self,
281282
return Schema_L2TP(
282283
flags={
283284
'type': type_,
284-
'len': length is not None,
285+
'len': total_length is not None,
285286
'seq': ns is not None and nr is not None,
286287
'offset': offset is not None,
287288
'prio': priority,
288289
'version': version,
289290
},
290-
length=length,
291+
length=total_length,
291292
tunnel_id=tunnel_id,
292293
session_id=session_id,
293294
ns=ns,
@@ -364,7 +365,11 @@ def _make_data(cls, data: 'Data_L2TP') -> 'dict[str, Any]': # type: ignore[over
364365
'type': data.flags.type,
365366
'priority': data.flags.prio,
366367
'version': data.version,
367-
'length': data.length,
368+
# NOTE: Passed as ``total_length`` since ``length`` is the parse
369+
# length :class:`~pcapkit.protocols.protocol.ProtocolBase` takes, so
370+
# under that key it never reached :meth:`make`, and the rebuild
371+
# dropped both the Length field and the ``L`` flag (:issue:`1172`).
372+
'total_length': data.length,
368373
'tunnel_id': data.tunnelid,
369374
'session_id': data.sessionid,
370375
'ns': data.ns,
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# -*- coding: utf-8 -*-
2+
"""L2TPv2 keeps the ``L`` flag and the Length field through ``from_data``.
3+
4+
GitHub issue #1172: :meth:`L2TPv2._make_data
5+
<pcapkit.protocols.link.l2tpv2.L2TPv2._make_data>` returned the parsed Length
6+
under ``length``, which is the parse length
7+
:class:`~pcapkit.protocols.protocol.ProtocolBase` takes for itself, so it never
8+
reached :meth:`~pcapkit.protocols.link.l2tpv2.L2TPv2.make`. The rebuild then
9+
cleared ``L`` and omitted the two Length octets: ``c802000c0001000000010002``
10+
came back as ``88020001000000010002``.
11+
12+
Every case builds its own octets in memory and reads no capture.
13+
14+
:class:`L2TPv2` is imported inside each test, after
15+
:func:`~tests._support.reimport_once_per_class`, not at module load, for the
16+
reason :mod:`tests.protocols.link.test_ethernet_mac_roundtrip_unit` gives.
17+
18+
"""
19+
20+
import io
21+
import itertools
22+
import unittest
23+
24+
from tests._support import reimport_once_per_class
25+
26+
#: A PPP frame header, as the L2TPv2 payload.
27+
PAYLOAD = bytes.fromhex('ff030021')
28+
29+
30+
def build(type_: int, len_: int, seq: int, offset: int, pad: int) -> bytes:
31+
"""Build an L2TPv2 datagram with the given optional fields present."""
32+
flags = (type_ << 15) | (len_ << 14) | (seq << 11) | (offset << 9) | 2
33+
hdr_len = 6 + 2 * (len_ + 2 * seq + offset) + pad
34+
body = b''
35+
if len_:
36+
body += (hdr_len + len(PAYLOAD)).to_bytes(2, 'big')
37+
body += (7).to_bytes(2, 'big') + (9).to_bytes(2, 'big')
38+
if seq:
39+
body += (3).to_bytes(2, 'big') + (4).to_bytes(2, 'big')
40+
if offset:
41+
body += pad.to_bytes(2, 'big') + bytes(pad)
42+
return flags.to_bytes(2, 'big') + body + PAYLOAD
43+
44+
45+
#: Every combination of ``T``, ``L``, ``S`` and ``O``; with ``O``, both an
46+
#: empty and a three-octet (zero) offset pad.
47+
COMBINATIONS = [
48+
(t, l, s, o, pad)
49+
for t, l, s, o in itertools.product((0, 1), repeat=4)
50+
for pad in ((0, 3) if o else (0,))
51+
]
52+
53+
54+
class TestL2TPv2LengthRoundTrip(unittest.TestCase):
55+
"""Pin that the Length field survives a ``from_data`` rebuild."""
56+
57+
def setUp(self) -> None:
58+
reimport_once_per_class(self)
59+
60+
def test_issue_reproduction(self) -> None:
61+
from pcapkit.protocols.link.l2tpv2 import L2TPv2
62+
63+
raw = bytes.fromhex('c802000c0001000000010002') # ZLB ACK: T, L and S set
64+
parsed = L2TPv2(raw)
65+
self.assertTrue(parsed.info.flags.len)
66+
self.assertEqual(parsed.info.length, 12)
67+
self.assertEqual(L2TPv2.from_data(parsed.info).data, raw)
68+
69+
def test_every_flag_combination_round_trips(self) -> None:
70+
from pcapkit.protocols.link.l2tpv2 import L2TPv2
71+
72+
self.assertEqual(len(COMBINATIONS), 24)
73+
for t, l, s, o, pad in COMBINATIONS:
74+
raw = build(t, l, s, o, pad)
75+
with self.subTest(T=t, L=l, S=s, O=o, pad=pad, raw=raw.hex()):
76+
parsed = L2TPv2(io.BytesIO(raw), len(raw))
77+
self.assertEqual(L2TPv2.from_data(parsed.info).data, raw)
78+
79+
def test_make_data_passes_the_length_as_total_length(self) -> None:
80+
from pcapkit.protocols.link.l2tpv2 import L2TPv2
81+
82+
values = L2TPv2._make_data(L2TPv2(build(1, 1, 1, 0, 0)).info)
83+
self.assertNotIn('length', values)
84+
self.assertEqual(values['total_length'], 16)
85+
86+
values = L2TPv2._make_data(L2TPv2(build(0, 0, 0, 0, 0)).info)
87+
self.assertIsNone(values['total_length'])
88+
89+
def test_make_sets_l_flag_only_with_total_length(self) -> None:
90+
from pcapkit.protocols.link.l2tpv2 import L2TPv2
91+
92+
# ``type`` is passed as the raw ``T`` bit so the octets do not depend on
93+
# how :class:`~pcapkit.const.l2tp.type.Type` names it.
94+
with_len = L2TPv2(type=0, tunnel_id=7, session_id=9, total_length=12,
95+
payload=PAYLOAD)
96+
self.assertEqual(with_len.data, build(0, 1, 0, 0, 0))
97+
self.assertTrue(with_len.info.flags.len)
98+
self.assertEqual(with_len.info.length, 12)
99+
100+
without = L2TPv2(type=0, tunnel_id=7, session_id=9, payload=PAYLOAD)
101+
self.assertEqual(without.data, build(0, 0, 0, 0, 0))
102+
self.assertFalse(without.info.flags.len)
103+
self.assertIsNone(without.info.length)
104+
105+
106+
if __name__ == '__main__':
107+
unittest.main()

‎tests/protocols/link/test_link_unit.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def test_l2tp_properties_read_and_make_variants(self) -> None:
678678
schema = maker.make(
679679
type=Type.Control,
680680
priority=True,
681-
length=20,
681+
total_length=20,
682682
tunnel_id=7,
683683
session_id=8,
684684
ns=9,

0 commit comments

Comments
 (0)