Skip to content

Commit c7cec9c

Browse files
authored
fix(ipv6-route): rebuild RPL source routes byte for byte (#1456, #1457) (#1463)
- Compress a parsed address again on rebuild: Addresses[n] by CmprE, the rest by CmprI, so an RPL header inside IPv6 keeps its width (#1456). - Split the address list by the span Hdr Ext Len declares, not by the octets read; an address the capture cut short stays the raw bytes read instead of decoding as IPv4 or raising a builtins ValueError (#1457). - Pass the parsed Hdr Ext Len through from_data via a new make() hdr_ext_len argument, so a truncated header keeps its length (#1457). - make(): clamp CmprI/CmprE to 15, set CmprI to 0 for one address, and raise ProtocolError for an empty address list. New tests/protocols/internet/test_ipv6_route_rpl_roundtrip_unit.py.
1 parent 5132e93 commit c7cec9c

3 files changed

Lines changed: 245 additions & 30 deletions

File tree

‎pcapkit/protocols/internet/ipv6_route.py‎

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ def make(self,
259259
type_reversed: 'bool' = False,
260260
seg_left: 'int' = 0,
261261
data: 'bytes | Data_IPv6_Route | Schema_RoutingType | dict[str, Any]' = b'\x00\x00\x00\x00',
262+
hdr_ext_len: 'Optional[int]' = None,
262263
payload: 'ProtocolBase | Schema | bytes' = b'',
263264
**kwargs: 'Any') -> 'Schema_IPv6_Route':
264265
"""Make (construct) packet data.
@@ -275,6 +276,8 @@ def make(self,
275276
type_reversed: If the namespace of routing type is reversed.
276277
seg_left: Segments left.
277278
data: Routing data.
279+
hdr_ext_len: ``Hdr Ext Len`` field value; computed from ``data``
280+
when omitted.
278281
payload: Payload of current instance.
279282
**kwargs: Arbitrary keyword arguments.
280283
@@ -336,6 +339,13 @@ def make(self,
336339
else:
337340
raise ProtocolError(f'{self.alias}: invalid routing data type: {data.__class__}')
338341

342+
# NOTE: An explicit ``hdr_ext_len`` is written as given, so that a
343+
# ``from_data`` rebuild of a header the capture cut short keeps the
344+
# length the sender declared instead of the one that was recorded
345+
# (:issue:`1457`), the same as IPv6's ``payload_length``.
346+
if hdr_ext_len is not None:
347+
length = hdr_ext_len
348+
339349
return Schema_IPv6_Route(
340350
next=next_val,
341351
length=length,
@@ -450,6 +460,11 @@ def _make_data(cls, data: 'Data_IPv6_Route') -> 'dict[str, Any]': # type: ignor
450460
'type': data.type,
451461
'seg_left': data.seg_left,
452462
'data': data,
463+
# NOTE: ``data.length`` is the parsed header length in octets,
464+
# passed through as ``Hdr Ext Len`` rather than left to ``make`` to
465+
# recompute, so that a truncated capture rebuilds with the length
466+
# that was on the wire (:issue:`1457`).
467+
'hdr_ext_len': (data.length - 8) // 8,
453468
'payload': cls._make_payload(data),
454469
}
455470

@@ -784,6 +799,7 @@ def _make_data_type_rpl(self, type: 'Enum_Routing', route: 'Optional[Data_RPL]'
784799
Raises:
785800
FieldValueError: If an entry of ``ip`` is a :obj:`bool` (c.f.
786801
:func:`~pcapkit.corekit.fields.ipaddress.parse_ip_address`).
802+
ProtocolError: If ``ip`` is empty.
787803
788804
"""
789805
if route is not None:
@@ -792,8 +808,20 @@ def _make_data_type_rpl(self, type: 'Enum_Routing', route: 'Optional[Data_RPL]'
792808
pad = route.pad
793809
reserved = route.reserved
794810
padding = getattr(route, 'padding', b'')
811+
812+
# NOTE: A parsed address is decoded to its full 16 octets whenever
813+
# the header sat inside IPv6, the ``dst`` prefix restored, so it is
814+
# compressed again here: ``Addresses[n]`` by ``CmprE``, every other
815+
# one by ``CmprI``. Which item is ``Addresses[n]`` comes from the
816+
# count ``Hdr Ext Len`` declares (:rfc:`6554#section-4.2`), since a
817+
# capture cut inside the list holds fewer; one it cut short is left
818+
# as the raw :obj:`bytes` read and written back as is.
819+
area = route.length - 8 - pad
820+
count = max(area - (16 - cmpr_e), 0) // (16 - cmpr_i) + 1
821+
last = max(len(route.ip), count) - 1
795822
ip_val = [
796-
addr if isinstance(addr, bytes) else addr.packed for addr in route.ip
823+
addr if isinstance(addr, bytes) else addr.packed[cmpr_e if index == last else cmpr_i:]
824+
for index, addr in enumerate(route.ip)
797825
]
798826
else:
799827
ip = [] if ip is None else ip
@@ -806,6 +834,12 @@ def _make_data_type_rpl(self, type: 'Enum_Routing', route: 'Optional[Data_RPL]'
806834
# address of ``00000001`` with ``cmpr_e=0``.
807835
descr = f'{self.alias}: invalid RPL source address'
808836

837+
# NOTE: :rfc:`6554#section-4.2` counts ``n`` as ``(...) + 1``, so
838+
# a header always carries ``Addresses[n]``; one with none would not
839+
# parse back.
840+
if not ip:
841+
raise ProtocolError(f'{self.alias}: [TypeNo {type}] RPL source route needs at least one address')
842+
809843
if dst is None:
810844
pad = 0
811845
cmpr_i = 0
@@ -821,15 +855,21 @@ def _make_data_type_rpl(self, type: 'Enum_Routing', route: 'Optional[Data_RPL]'
821855
else:
822856
test_list.append(cast('IPv6Address', parse_ip_address(item, descr, version=6)).packed)
823857
prefix_i = os_path.commonprefix(test_list)
824-
cmpr_i = len(prefix_i)
858+
859+
# NOTE: ``CmprI`` and ``CmprE`` are 4-bit fields
860+
# (:rfc:`6554#section-3`), so a prefix of all 16 octets -- an
861+
# address equal to ``dst``, or no ``Addresses[1..n-1]`` at all,
862+
# which leaves ``dst`` alone in the list -- is elided as 15. With
863+
# no ``Addresses[1..n-1]``, ``CmprI`` governs nothing and is 0.
864+
cmpr_i = min(len(prefix_i), 15) if len(ip) > 1 else 0
825865

826866
test_list = [dst.packed]
827867
if isinstance(ip[-1], bytes):
828868
test_list.append(ip[-1])
829869
else:
830870
test_list.append(cast('IPv6Address', parse_ip_address(ip[-1], descr, version=6)).packed)
831871
prefix_e = os_path.commonprefix(test_list)
832-
cmpr_e = len(prefix_e)
872+
cmpr_e = min(len(prefix_e), 15)
833873

834874
# NOTE: the outer ``% 8`` keeps an already 8-octet-aligned
835875
# vector from being handed a *full* 8 octets of padding

‎pcapkit/protocols/schema/internet/ipv6_route.py‎

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,24 @@ class RPL(RoutingType, code=Enum_Routing.RPL_Source_Route_Header):
215215
#: Padding.
216216
padding: 'bytes' = PaddingField(length=lambda pkt: pkt['pad']['pad_len'])
217217

218+
@classmethod
219+
def pre_unpack(cls, packet: 'dict[str, Any]') -> 'None':
220+
"""Prepare ``packet`` data for unpacking process.
221+
222+
Args:
223+
packet: packet data
224+
225+
"""
226+
# NOTE: The declared span -- ``4 + Hdr Ext Len * 8`` octets inside
227+
# IPv6-Route -- is kept for :meth:`post_process`, which splits
228+
# ``Addresses[1..n]`` by it. By then ``__length__`` has been counted
229+
# down to zero, and the octets actually read are fewer than declared
230+
# whenever the capture ends inside the address list. :meth:`pack` calls
231+
# this too, before it has set ``__length__``; the packing path does not
232+
# need the span, so nothing is kept there.
233+
if '__length__' in packet:
234+
packet['__rpl_length__'] = packet['__length__']
235+
218236
def post_process(self, packet: 'dict[str, Any]') -> 'Schema':
219237
"""Revise ``schema`` data after unpacking process.
220238
@@ -270,39 +288,43 @@ def post_process(self, packet: 'dict[str, Any]') -> 'Schema':
270288

271289
ilen = 16 - cmpr_i
272290
elen = 16 - cmpr_e
291+
292+
# NOTE: The address list is split by the span ``Hdr Ext Len`` declares,
293+
# not by the octets actually read: once a capture ends inside the list
294+
# the two differ, and splitting by the latter decodes the octets of
295+
# ``Addresses[1]`` as ``Addresses[n]`` (or a short run as an IPv4
296+
# address). The declared span is what :attr:`addresses`'s own
297+
# ``length`` callback asked for -- ``pad_len`` already subtracted, the
298+
# trailing padding octets being read by :attr:`padding` -- and a
299+
# malformed one (no whole number of addresses) is rejected by
300+
# :meth:`~pcapkit.protocols.internet.ipv6_route.IPv6_Route._read_data_type_rpl`,
301+
# so it is only clamped here.
302+
declared = packet.get('__rpl_length__', len(buffer) + 4 + self.pad['pad_len'])
303+
area = max(declared - 4 - self.pad['pad_len'], 0)
304+
widths = [ilen] * (max(area - elen, 0) // ilen) + [elen]
305+
306+
# NOTE: Only an item read at its full width is decoded. One the capture
307+
# cut short stays the raw :obj:`bytes` that were read -- not prefixed
308+
# with ``dst``, so a rebuild writes back exactly those octets -- and
309+
# one cut off entirely is not listed at all.
273310
addr = [] # type: list[IPv6Address | bytes]
274311
counter = 0
312+
for index, width in enumerate(widths):
313+
buf = buffer[counter:counter + width]
314+
counter += width
315+
if not buf:
316+
break
317+
if len(buf) < width:
318+
addr.append(buf)
319+
break
275320

276-
# Addresses[1..n-1]
277-
#
278-
# NOTE: ``buffer`` is ``self.addresses``, whose own ``length`` callback
279-
# above already subtracted ``pad_len`` -- the trailing padding octets
280-
# are read by :attr:`padding`, not by this field. Subtracting
281-
# ``pad_len`` a *second* time here would drop one address for every
282-
# ``ilen`` octets of padding, so a padded (i.e. compressed) header
283-
# would parse one address short.
284-
for _ in range((len(buffer) - elen) // ilen):
285-
buf = buffer[counter:counter + ilen]
286-
if dst is None:
287-
if cmpr_i == 0:
288-
addr.append(cast('IPv6Address', ipaddress.ip_address(buf)))
289-
else:
290-
addr.append(buf)
291-
else:
292-
buf = dst[:cmpr_i] + buf
293-
addr.append(cast('IPv6Address', ipaddress.ip_address(buf)))
294-
counter += ilen
295-
296-
# Addresses[n]
297-
buf = buffer[counter:counter + elen]
298-
if dst is None:
299-
if cmpr_e == 0:
321+
cmpr = cmpr_e if index == len(widths) - 1 else cmpr_i
322+
if dst is not None:
323+
addr.append(cast('IPv6Address', ipaddress.ip_address(dst[:cmpr] + buf)))
324+
elif cmpr == 0:
300325
addr.append(cast('IPv6Address', ipaddress.ip_address(buf)))
301326
else:
302327
addr.append(buf)
303-
else:
304-
buf = dst[:cmpr_e] + buf
305-
addr.append(cast('IPv6Address', ipaddress.ip_address(buf)))
306328

307329
self.ip = addr
308330
return self
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# -*- coding: utf-8 -*-
2+
"""An RPL source route header (IPv6-Route type 3) rebuilds byte for byte.
3+
4+
GitHub issue #1456: inside IPv6, the parser restores the ``dst`` prefix of each
5+
compressed address, and :meth:`IPv6_Route._make_data_type_rpl
6+
<pcapkit.protocols.internet.ipv6_route.IPv6_Route._make_data_type_rpl>` wrote
7+
the decoded address back at its full 16 octets, so a header with ``CmprI`` or
8+
``CmprE`` above zero grew on rebuild.
9+
10+
GitHub issue #1457: a header the capture cut inside its address list was split
11+
by the octets read rather than by ``Hdr Ext Len``, so a short run decoded as an
12+
IPv4 address or raised a builtins :exc:`ValueError`, and the rebuild wrote a
13+
smaller ``Hdr Ext Len``.
14+
15+
A cut that leaves ``Pad`` octets unread still rebuilds with them zero-filled;
16+
that is the corekit short-read defect, #1458, so those cases only check that
17+
the octets read come back first and ``Hdr Ext Len`` is kept.
18+
19+
Every case builds its own octets in memory and reads no capture. Classes are
20+
imported inside each test, after :func:`~tests._support.reimport_once_per_class`.
21+
22+
"""
23+
24+
import ipaddress
25+
import unittest
26+
import warnings
27+
28+
from tests._support import reimport_once_per_class
29+
30+
SRC = ipaddress.IPv6Address('2001:db8::1').packed
31+
DST = ipaddress.IPv6Address('2001:db8:aa:bb:cc:dd:ee:ff').packed
32+
33+
34+
def ipv6(route: bytes) -> bytes:
35+
"""Wrap ``route`` in an IPv6 header from ``SRC`` to ``DST``."""
36+
return (bytes([0x60, 0, 0, 0]) + len(route).to_bytes(2, 'big') + bytes([43, 64])
37+
+ SRC + DST + route)
38+
39+
40+
def rpl(cmpr_i: int, cmpr_e: int, count: int, extra_pad: int = 0) -> bytes:
41+
"""Build an RPL header carrying ``count`` addresses."""
42+
area = (count - 1) * (16 - cmpr_i) + (16 - cmpr_e)
43+
pad = (-area) % 8 + extra_pad
44+
body = bytes((i * 7 + 3) % 256 for i in range(area))
45+
hdr_ext_len = (area + pad) // 8
46+
return bytes([59, hdr_ext_len, 3, count, (cmpr_i << 4) | cmpr_e, pad << 4, 0, 0]) + body + bytes(pad)
47+
48+
49+
def headers():
50+
"""Every ``CmprI``/``CmprE`` pair, one to three addresses, minimal and widened ``Pad``."""
51+
for cmpr_i in range(16):
52+
for cmpr_e in range(16):
53+
for count in (1, 2, 3):
54+
minimal = (-((count - 1) * (16 - cmpr_i) + (16 - cmpr_e))) % 8
55+
for extra in (0, 8):
56+
if minimal + extra <= 15:
57+
yield (cmpr_i, cmpr_e, count, extra), rpl(cmpr_i, cmpr_e, count, extra)
58+
59+
60+
class TestIPv6RouteRPLRoundTrip(unittest.TestCase):
61+
"""Pin the rebuild of RPL source route headers, on their own and inside IPv6."""
62+
63+
def setUp(self) -> None:
64+
reimport_once_per_class(self)
65+
warnings.simplefilter('ignore')
66+
self.addCleanup(warnings.resetwarnings)
67+
68+
def rebuild(self, cls, raw: bytes) -> bytes:
69+
return cls.from_data(cls(raw, len(raw)).info).data
70+
71+
def test_issue_1456_compressed_header_inside_ipv6(self) -> None:
72+
from pcapkit.protocols.internet.ipv6 import IPv6
73+
74+
raw = ipv6(bytes([59, 2, 3, 1, 0x88, 0, 0, 0]) + bytes(range(1, 17)))
75+
self.assertEqual(len(raw), 64)
76+
self.assertEqual(self.rebuild(IPv6, raw), raw)
77+
78+
def test_issue_1457_cut_address_is_kept_as_read(self) -> None:
79+
from pcapkit.protocols.internet.ipv6_route import IPv6_Route
80+
81+
route = bytes([59, 2, 3, 1, 0, 0, 0, 0]) + bytes(range(1, 17))
82+
for cut in (12, 23):
83+
with self.subTest(cut=cut):
84+
info = IPv6_Route(route[:cut], cut).info
85+
self.assertEqual(info.ip, (route[8:cut],))
86+
self.assertEqual(self.rebuild(IPv6_Route, route[:cut]), route[:cut])
87+
88+
def test_issue_1457_cut_keeps_hdr_ext_len(self) -> None:
89+
from pcapkit.protocols.internet.ipv6_route import IPv6_Route
90+
91+
route = bytes([59, 2, 3, 1, 0x88, 0, 0, 0]) + bytes(range(1, 17))
92+
self.assertEqual(self.rebuild(IPv6_Route, route[:12]), route[:12])
93+
94+
def test_every_compression_rebuilds(self) -> None:
95+
from pcapkit.protocols.internet.ipv6 import IPv6
96+
from pcapkit.protocols.internet.ipv6_route import IPv6_Route
97+
98+
for label, route in headers():
99+
with self.subTest(case=label):
100+
self.assertEqual(self.rebuild(IPv6_Route, route), route)
101+
self.assertEqual(self.rebuild(IPv6, ipv6(route)), ipv6(route))
102+
103+
def test_every_cut_rebuilds(self) -> None:
104+
from pcapkit.protocols.internet.ipv6 import IPv6
105+
from pcapkit.protocols.internet.ipv6_route import IPv6_Route
106+
107+
# Two addresses put a cut in both ``Addresses[1..n-1]`` and ``Addresses[n]``.
108+
for label, route in headers():
109+
if label[2:] != (2, 0):
110+
continue
111+
pad = route[5] >> 4
112+
for cut in range(8, len(route)):
113+
with self.subTest(case=label, cut=cut):
114+
alone = self.rebuild(IPv6_Route, route[:cut])
115+
inner = self.rebuild(IPv6, ipv6(route)[:40 + cut])
116+
if pad == 0:
117+
self.assertEqual(alone, route[:cut])
118+
self.assertEqual(inner, ipv6(route)[:40 + cut])
119+
else: # unread ``Pad`` octets are zero-filled (#1458)
120+
self.assertEqual(alone[:cut], route[:cut])
121+
self.assertEqual(inner[:40 + cut], ipv6(route)[:40 + cut])
122+
self.assertEqual(alone[1], route[1])
123+
124+
def test_make_parse_rebuilds(self) -> None:
125+
from pcapkit.protocols.internet.ipv6 import IPv6
126+
from pcapkit.protocols.internet.ipv6_route import IPv6_Route
127+
128+
dst = ipaddress.IPv6Address(DST)
129+
for shared in (0, 4, 8, 15, 16):
130+
for count in (1, 2, 3):
131+
addrs = [ipaddress.IPv6Address(DST[:shared] + bytes([0x40 + j]) * (16 - shared))
132+
for j in range(count)]
133+
with self.subTest(shared=shared, count=count):
134+
made = IPv6_Route(type=3, dst=dst, data={'ip': addrs}, seg_left=count).data
135+
self.assertEqual(self.rebuild(IPv6_Route, made), made)
136+
packet = ipv6(made)
137+
self.assertEqual(self.rebuild(IPv6, packet), packet)
138+
route = list(IPv6(packet, len(packet)).extension_headers.values())[0].info
139+
self.assertEqual(list(route.ip), addrs)
140+
self.assertLessEqual(route.cmpr_e, 15)
141+
142+
def test_make_without_addresses_is_rejected(self) -> None:
143+
from pcapkit.protocols.internet.ipv6_route import IPv6_Route
144+
from pcapkit.utilities.exceptions import ProtocolError
145+
146+
for dst in (None, ipaddress.IPv6Address(DST)):
147+
with self.subTest(dst=dst):
148+
with self.assertRaisesRegex(ProtocolError, 'needs at least one address'):
149+
IPv6_Route(type=3, dst=dst, data={'ip': []})
150+
151+
152+
if __name__ == '__main__':
153+
unittest.main()

0 commit comments

Comments
 (0)