Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 10 additions & 7 deletions docs/source/pcapkit/foundation/engines/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,16 @@ offers says so rather than quietly doing less, unless its row says otherwise:
| :class:`~pcapkit.foundation.engines.dpkt.DPKT` | TCP or UDP in an IP-in-IP tunnel, or behind an AH or IPv6 |
| | extension header carried in IPv4, is left out of TCP |
| | reassembly and TCP or UDP flow tracing, and IPv4 carried in |
| | IPv6, or IPv6 in IPv4, out of IP reassembly, and, as `DPKT`_ |
| | pads DATA chunks alone, an SCTP chunk after any other whose |
| | Length is no multiple of four out of SCTP reassembly, as is a |
| | packet ending in one, which it decodes no SCTP layer from, |
| | all without a warning; a frame `DPKT`_ cannot decode is kept |
| | raw, with a :class:`~pcapkit.utilities.warnings.DPKTWarning`, |
| | and is neither reassembled nor traced |
| | IPv6, or IPv6 in IPv4, out of IP reassembly, and, behind some |
| | stacks of VLAN tags whose layers `DPKT`_'s own lengths |
| | misplace (:issue:`1646`), the SCTP chunks after a non-DATA |
| | chunk whose Length is no multiple of four out of SCTP |
| | reassembly, though it reads the DATA chunks of a packet the |
| | default engine refuses for a last chunk cut inside its |
| | padding, all without a warning; a frame `DPKT`_ cannot |
| | decode is kept raw, with a |
| | :class:`~pcapkit.utilities.warnings.DPKTWarning`, and is |
| | neither reassembled nor traced |
+-----------------------------------------------------------------+---------------------------------------------------------------+
| :class:`~pcapkit.foundation.engines.scapy.Scapy` | reads IPv6 only as far as `Scapy`_ dissects it, without a |
| | warning: behind a BSD loopback header only at the family |
Expand Down
2 changes: 2 additions & 0 deletions docs/source/pcapkit/toolkit/3rdparty.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Auxiliary Functions

.. autofunction:: pcapkit.toolkit.dpkt._sctp_carrier

.. autofunction:: pcapkit.toolkit.dpkt._sctp_chunks

.. autofunction:: pcapkit.toolkit.dpkt._sctp_fragment

Auxiliary Data
Expand Down
110 changes: 88 additions & 22 deletions pcapkit/toolkit/dpkt.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,15 +851,16 @@ def tcp_reassembly(packet: 'Packet', timestamp: 'float', *,
return None


def _sctp_carrier(packet: 'Packet') -> 'Optional[tuple[IP | IP6, SCTP]]':
def _sctp_carrier(packet: 'Packet') -> 'Optional[tuple[IP | IP6, SCTP | bytes]]':
"""Fetch the SCTP layer of a DPKT packet, and the IP layer that carries it.

Args:
packet: DPKT packet, i.e. the outermost layer.

Returns:
The first SCTP layer (:class:`dpkt.sctp.SCTP`), from the network layer in,
and the IPv4 or IPv6 layer whose payload it is; or :data:`None` if there is
or the octets `DPKT`_ kept of an SCTP packet it decoded no layer from, and
the IPv4 or IPv6 layer whose payload it is; or :data:`None` if there is
none.

Note:
Expand All @@ -879,12 +880,87 @@ def _sctp_carrier(packet: 'Packet') -> 'Optional[tuple[IP | IP6, SCTP]]':
if isinstance(layer.data, dpkt.sctp.SCTP):
return layer, layer.data
# ``IP6.p`` is the protocol after the extension headers
if getattr(layer, 'p', None) not in (Enum_TransType.IPv4, Enum_TransType.IPv6):
protocol = getattr(layer, 'p', None)
if protocol == Enum_TransType.SCTP and isinstance(layer.data, bytes):
# NOTE: dpkt keeps the octets of an SCTP packet it cannot decode, and of a later
# fragment, which holds no SCTP packet; 44 is an IPv6 Fragment header.
offset = (getattr(layer.extension_hdrs.get(44), 'frag_off', 0)
if isinstance(layer, dpkt.ip6.IP6) else layer.offset)
return None if offset else (layer, layer.data)
if protocol not in (Enum_TransType.IPv4, Enum_TransType.IPv6):
break
layer = layer.data
return None


def _sctp_chunks(packet: 'Packet', ip: 'IP | IP6',
sctp: 'SCTP | bytes') -> 'Optional[tuple[tuple[int, int, int], list[Chunk]]]':
"""Fetch the buffer ID and the chunks of an SCTP packet.

Args:
packet: DPKT packet, i.e. the outermost layer.
ip: IPv4 or IPv6 layer carrying the SCTP packet (see :func:`_sctp_carrier`).
sctp: Its SCTP layer (:class:`dpkt.sctp.SCTP`), or the octets `DPKT`_ kept
where it decoded none.

Returns:
The SCTP ports and verification tag, and the chunks
(:class:`dpkt.sctp.Chunk`) in order: the SCTP layer's own, or, where `DPKT`_
misplaced one or decoded no layer, those walked again from the SCTP packet's
octets, each padded to a multiple of four octets (:rfc:`9260#section-3.2`),
up to a Length shorter than the chunk header. :data:`None` if the octets are
shorter than the common header.

Note:
`DPKT`_ reads the padding after a DATA chunk alone, so it misplaces the
chunks after any other chunk whose Length is not a multiple of four, an
I-DATA chunk among them, and decodes no SCTP layer where such a chunk ends
the packet. The octets of a layer it decoded are sliced out of the record
octets :class:`~pcapkit.foundation.engines.dpkt.DPKT` attached to the frame,
to the end of the IP payload as `DPKT`_ cuts it. Where those are not at hand
(see :func:`_layer2bytes`), as for a packet built by hand or behind some
stacks of VLAN tags (:issue:`1646`), the chunks stop at the one after which
`DPKT`_ misplaced the rest.

.. versionadded:: 1.10.0

"""
import dpkt # isort:skip

if isinstance(sctp, dpkt.sctp.SCTP):
bufid = (sctp.sport, sctp.dport, sctp.vtag)
chunks = getattr(sctp, 'chunks', []) # type: list[Chunk]
# NOTE: a chunk built by hand has no ``padding``, as dpkt never walked to it.
misplaced = next((index for (index, chunk) in enumerate(chunks)
if getattr(chunk, 'padding', None) is not None
and len(chunk.padding) != pad_length(chunk.len, 4)), None)
if misplaced is None:
return bufid, chunks
wire, captured = _layer2bytes(packet, ip)
if not captured:
return bufid, chunks[:misplaced + 1]
# dpkt runs a zero Total Length or Payload Length to the end of the frame
if isinstance(ip, dpkt.ip6.IP6):
end = ip.__hdr_len__ + ip.plen if ip.plen else None # type: Optional[int]
else:
end = ip.len or None
octets = wire[_wire_hdr_len(ip, sctp):end]
else:
octets = sctp
# NOTE: the common header is 12 octets and a chunk header 4 (RFC 9260 3.1, 3.2).
if len(octets) < 12:
return None

walked, offset = [], 12 # type: list[Chunk], int
while len(octets) - offset >= 4:
length = int.from_bytes(octets[offset + 2:offset + 4], 'big')
if length < 4:
break
walked.append(dpkt.sctp.Chunk(octets[offset:]))
offset += length + pad_length(length, 4)
return cast('tuple[int, int, int]', struct.unpack_from('!HHI', octets)), walked


def _sctp_fragment(chunk: 'Chunk', *, bufid: 'tuple[int, int, int]', src: 'IPv4Address | IPv6Address',
dst: 'IPv4Address | IPv6Address', num: 'int', timestamp: 'float') -> 'Optional[SCTP_Packet]':
"""Make data for SCTP user-message reassembly from a DPKT SCTP chunk.
Expand Down Expand Up @@ -959,10 +1035,10 @@ def sctp_reassembly(packet: 'Packet', timestamp: 'float', *,
count: 'int' = -1) -> 'Optional[tuple[SCTP_Packet, ...]]':
"""Make data for SCTP user-message reassembly.

A packet can be reassembled if an IPv4 or IPv6 layer carries an SCTP layer
(:class:`dpkt.sctp.SCTP`, see :func:`_sctp_carrier`) bundling a DATA or I-DATA
chunk that is a fragment of a user message (:rfc:`9260#section-6.9`,
:rfc:`8260#section-2.2.3`; see :func:`_sctp_fragment`).
A packet can be reassembled if an IPv4 or IPv6 layer carries an SCTP packet
(see :func:`_sctp_carrier`) bundling a DATA or I-DATA chunk that is a fragment
of a user message (:rfc:`9260#section-6.9`, :rfc:`8260#section-2.2.3`; see
:func:`_sctp_fragment`). The chunks are those :func:`_sctp_chunks` walks.

Args:
packet: DPKT packet.
Expand All @@ -976,14 +1052,6 @@ def sctp_reassembly(packet: 'Packet', timestamp: 'float', *,
tag, and each record also carries the addresses of the IP layer carrying
the SCTP one.

Note:
Every chunk is padded to a multiple of four octets
(:rfc:`9260#section-3.2`), but `DPKT`_ reads the padding after a DATA chunk
alone. So it misplaces the chunks after any other chunk whose Length is not
a multiple of four, an I-DATA chunk among them, and those are not read; and
where such a chunk ends the packet, the padding left over makes `DPKT`_
decode no SCTP layer at all.

See Also:
:class:`pcapkit.foundation.reassembly.sctp.reassembly.SCTP`

Expand All @@ -994,20 +1062,18 @@ def sctp_reassembly(packet: 'Packet', timestamp: 'float', *,
if found is None:
return None
ip, sctp = found
walked = _sctp_chunks(packet, ip, sctp)
if walked is None:
return None
bufid, chunks = walked

bufid = (sctp.sport, sctp.dport, sctp.vtag)
src, dst = ipaddress.ip_address(ip.src), ipaddress.ip_address(ip.dst)
packets = [] # type: list[SCTP_Packet]
for chunk in getattr(sctp, 'chunks', ()):
for chunk in chunks:
record = _sctp_fragment(chunk, bufid=bufid, src=src, dst=dst, num=count,
timestamp=_capture_time(timestamp))
if record is not None:
packets.append(record)
# NOTE: dpkt pads a DATA chunk alone (RFC 9260 3.2), so misplaces what follows another
# unaligned one; a chunk built by hand has no ``padding``, as dpkt never walked to it.
padding = getattr(chunk, 'padding', None)
if padding is not None and len(padding) != pad_length(chunk.len, 4):
break
return tuple(packets) or None


Expand Down
1 change: 1 addition & 0 deletions tests/project/test_rfc_constants_one_home_1739_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@
'and the 16-bit window and MSS defaults'),
'pcapkit/protocols/transport/tcp/schema.py': ((6, 16, 28), 'TCP and MPTCP option sizes, and '
'the MP_ADD_ADDR lengths'),
'pcapkit/toolkit/dpkt.py': ((12,), 'the SCTP common header length (RFC 9260 section 3.1)'),
'pcapkit/toolkit/pyshark.py': ((6, 9, 20, 24, 28), 'Wireshark encapsulation numbers mapped '
'to link types'),
}
Expand Down
Loading
Loading