Skip to content
Merged
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
61 changes: 38 additions & 23 deletions docs/source/contributing/conventions/reassembly-integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ Parse Ownership
IPv6, :meth:`Transport.analyze <pcapkit.protocols.transport.transport.Transport.analyze>`
for TCP,
:meth:`DTLS.analyze_handshake <pcapkit.protocols.application.dtls.protocol.DTLS.analyze_handshake>`
for a DTLS handshake message, and
for a DTLS handshake message,
:meth:`SCTP.analyze <pcapkit.protocols.transport.sctp.protocol.SCTP.analyze>` for an
SCTP user message.
SCTP user message, and
:meth:`TLS.analyze_record <pcapkit.protocols.application.tls.protocol.TLS.analyze_record>`
for a TLS record (:issue:`2938`).
* Each :mod:`pcapkit.foundation.analysis` function is a public wrapper that calls that
method and holds no parsing of its own.
* The reassembly :class:`~pcapkit.foundation.reassembly.data.Deferred` calls the
Expand Down Expand Up @@ -105,9 +107,9 @@ Parse Ownership
TK["the pcapkit.toolkit.&lt;engine&gt; adapter the engine's<br/>\_\_reassembly\_\_ names, e.g. toolkit.pcap.dtls\_reassembly"]
RE["each enabled reassembler the Extractor holds,<br/>by its registered name, advanced to the frame's timestamp"]
DEF["Deferred: a datagram's packet,<br/>parsed on first read"]
AN["pcapkit.foundation.analysis<br/>ip\_datagram, tcp\_datagram, dtls\_handshake, sctp\_message<br/>public wrappers, no parsing"]
AN["pcapkit.foundation.analysis<br/>ip\_datagram, tcp\_datagram, dtls\_handshake,<br/>sctp\_message, tls\_record<br/>public wrappers, no parsing"]
subgraph protocols ["pcapkit.protocols"]
PM["the protocol class's parse method<br/>Protocol.analyze: IPv4, IPv6<br/>Transport.analyze: TCP<br/>DTLS.analyze\_handshake<br/>SCTP.analyze: SCTP"]
PM["the protocol class's parse method<br/>Protocol.analyze: IPv4, IPv6<br/>Transport.analyze: TCP<br/>DTLS.analyze\_handshake<br/>SCTP.analyze: SCTP<br/>TLS.analyze\_record"]
end
REG["pcapkit.foundation.registry.protocols"]

Expand All @@ -126,8 +128,9 @@ Compliance, Measured
:class:`~pcapkit.protocols.internet.ipv4.protocol.IPv4`,
:class:`~pcapkit.protocols.internet.ipv6.protocol.IPv6`,
:class:`~pcapkit.protocols.transport.tcp.protocol.TCP`,
:class:`~pcapkit.protocols.application.dtls.protocol.DTLS` and
:class:`~pcapkit.protocols.transport.sctp.protocol.SCTP` imports or calls into
:class:`~pcapkit.protocols.application.dtls.protocol.DTLS`,
:class:`~pcapkit.protocols.transport.sctp.protocol.SCTP` and
:class:`~pcapkit.protocols.application.tls.protocol.TLS` imports or calls into
:mod:`pcapkit.foundation.reassembly`. Instead :mod:`pcapkit.foundation.engines` calls a
``pcapkit.toolkit.<engine>`` extraction function on an already-parsed frame and feeds
the result to the reassembly instance the
Expand All @@ -141,7 +144,11 @@ that name, and each is advanced to every frame's timestamp before the frame is f
(:issue:`2814`).
:meth:`DTLS.read_handshake <pcapkit.protocols.application.dtls.protocol.DTLS.read_handshake>`
and :meth:`DTLS.analyze_handshake <pcapkit.protocols.application.dtls.protocol.DTLS.analyze_handshake>`
are the parser methods the rule allows.
are the parser methods the rule allows, and so are
:meth:`TLS.record_header <pcapkit.protocols.application.tls.protocol.TLS.record_header>`, which
reads the header a TLS stream is cut into records by, and
:meth:`TLS.analyze_record <pcapkit.protocols.application.tls.protocol.TLS.analyze_record>`
(:issue:`2938`).

**Point 2 holds through the engines, not through one** :mod:`pcapkit.toolkit`
**helper.** Each engine's :attr:`EngineBase.__reassembly__
Expand All @@ -153,7 +160,10 @@ declare which engines support a reassembler, and registering an adapter on an en
makes it support one. An enabled reassembler with no entry is
disabled with an :class:`~pcapkit.utilities.warnings.AttributeWarning`, and an engine
with no entries at all, as :mod:`pcapkit.foundation.engines.pyshark`, disables
reassembly (:issue:`2814`). DTLS and SCTP have entries on the default engine alone.
reassembly (:issue:`2814`). DTLS and SCTP have entries on the default engine alone. TLS has
entries on every engine with a TCP one: TLS reassembly reads TCP reassembly's record of each
segment, which each engine's ``tls_reassembly`` keeps for a stream on a port TCP binds to TLS
(:issue:`2938`).

**Flow tracing takes the same shape.** Every enabled flow tracer is made by the name it
is registered under in :attr:`Extractor.__traceflow__
Expand Down Expand Up @@ -189,21 +199,26 @@ datagram's by its ports, through
:func:`~pcapkit.foundation.analysis.dtls.dtls_handshake` decodes a reassembled
DTLS handshake message (:issue:`2496`), through
:meth:`DTLS.analyze_handshake <pcapkit.protocols.application.dtls.protocol.DTLS.analyze_handshake>`
(:issue:`2622`); and :func:`~pcapkit.foundation.analysis.sctp.sctp_message` parses a
(:issue:`2622`); :func:`~pcapkit.foundation.analysis.sctp.sctp_message` parses a
reassembled SCTP user message's by its payload protocol identifier, through
:meth:`SCTP.analyze <pcapkit.protocols.transport.sctp.protocol.SCTP.analyze>`. IP and
TCP have analysis modules of their own, each function named for the datagram its engine
reassembles, by the owner's choice on :issue:`2581`; without them the rule held for DTLS
alone.
:meth:`SCTP.analyze <pcapkit.protocols.transport.sctp.protocol.SCTP.analyze>`; and
:func:`~pcapkit.foundation.analysis.tls.tls_record` decodes a TLS record reassembled from a
TCP stream, through
:meth:`TLS.analyze_record <pcapkit.protocols.application.tls.protocol.TLS.analyze_record>`
(:issue:`2938`). IP and TCP have analysis modules of their own, each function named for the
datagram its engine reassembles, by the owner's choice on :issue:`2581`; without them the
rule held for DTLS alone.

**Where the rule is silent, DTLS takes the IP/TCP shape** (:issue:`2531`), and so does
SCTP. IPv4, IPv6, TCP, DTLS and SCTP reassembly each set ``__protocol_type__`` and
report the parsed outcome as a datagram's ``packet``, deferred to first read by
:class:`~pcapkit.foundation.reassembly.data.Deferred`. Each ``Deferred`` runs the
parse behind its engine's analysis function, so a ``packet`` is what that function
returns, and binds it to ``__protocol_type__`` -- ``analyze`` for IP and TCP
(:issue:`2581`) and for SCTP, ``analyze_handshake`` for DTLS (:issue:`2622`) -- so a
subclass naming another protocol has its ``packet`` parsed by that one. They differ on
an incomplete outcome: IP, TCP and SCTP parse a contiguous payload, complete or not, as
``strict=False`` reports one, and hold :data:`None` only for the received runs
``strict=True`` reports; DTLS holds :data:`None` for any incomplete message.
SCTP, and TLS takes DTLS's (:issue:`2938`). IPv4, IPv6, TCP, DTLS, SCTP and TLS reassembly
each set ``__protocol_type__`` and report the parsed outcome as a datagram's ``packet``,
deferred to first read by :class:`~pcapkit.foundation.reassembly.data.Deferred`. Each
``Deferred`` runs the parse behind its engine's analysis function, so a ``packet`` is what
that function returns, and binds it to ``__protocol_type__`` -- ``analyze`` for IP and TCP
(:issue:`2581`) and for SCTP, ``analyze_handshake`` for DTLS (:issue:`2622`),
``analyze_record`` for TLS -- so a subclass naming another protocol has its ``packet``
parsed by that one. They differ on an incomplete outcome: IP, TCP and SCTP parse a
contiguous payload, complete or not, as ``strict=False`` reports one, and hold :data:`None`
only for the received runs ``strict=True`` reports; DTLS holds :data:`None` for any
incomplete message, and TLS for any incomplete record, whose zero-filled octets would decode
as content.
1 change: 1 addition & 0 deletions docs/source/ext.rst
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ The reassembly and flow tracing classes, per protocol:
| :class:`~pcapkit.protocols.application.dtls.protocol.DTLS` | :class:`pcapkit.foundation.reassembly.dtls.reassembly.DTLS` | |
+------------------------------------------------------------+-------------------------------------------------------------+---------------------------------------------------------+
| :class:`~pcapkit.protocols.transport.sctp.protocol.SCTP` | :class:`pcapkit.foundation.reassembly.sctp.reassembly.SCTP` | |
| :class:`~pcapkit.protocols.application.tls.protocol.TLS` | :class:`pcapkit.foundation.reassembly.tls.reassembly.TLS` | |
+------------------------------------------------------------+-------------------------------------------------------------+---------------------------------------------------------+

.. seealso::
Expand Down
1 change: 1 addition & 0 deletions docs/source/pcapkit/foundation/analysis/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ itself is a method of the protocol class, which the function calls.
tcp
dtls
sctp
tls
15 changes: 15 additions & 0 deletions docs/source/pcapkit/foundation/analysis/tls.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
===================
TLS Record Analysis
===================

.. module:: pcapkit.foundation.analysis.tls

:mod:`pcapkit.foundation.analysis.tls` contains
:func:`~pcapkit.foundation.analysis.tls.tls_record` only, which decodes a TLS record
that :class:`~pcapkit.foundation.reassembly.tls.reassembly.TLS` reassembled from a TCP
stream (:rfc:`9846#section-5`). The decode itself is :meth:`TLS.analyze_record
<pcapkit.protocols.application.tls.protocol.TLS.analyze_record>`.

.. versionadded:: 1.9.0

.. autofunction:: pcapkit.foundation.analysis.tls.tls_record
2 changes: 1 addition & 1 deletion docs/source/pcapkit/foundation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Library Foundation
* the PCAP file extractor :class:`~pcapkit.foundation.extraction.Extractor`,
the :mod:`~pcapkit.foundation.options` it takes, and the
:mod:`~pcapkit.foundation.engines` it reads through;
* the IP, TCP, DTLS and SCTP :mod:`~pcapkit.foundation.reassembly`, and the
* the IP, TCP, DTLS, SCTP and TLS :mod:`~pcapkit.foundation.reassembly`, and the
:mod:`~pcapkit.foundation.analysis` of what it reassembles;
* the TCP flow tracer of :mod:`~pcapkit.foundation.traceflow`, and any other registered one;
* the :mod:`~pcapkit.foundation.registry` management APIs.
Expand Down
14 changes: 8 additions & 6 deletions docs/source/pcapkit/foundation/reassembly/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Packets Reassembly
.. module:: pcapkit.foundation.reassembly

:mod:`pcapkit.foundation.reassembly` implements datagram reassembly
of IP packets, TCP segments, DTLS handshake messages and SCTP user messages,
based on the algorithms described in :rfc:`791`, :rfc:`815`, :rfc:`6347`,
:rfc:`9260` and :rfc:`8260`.
of IP packets, TCP segments, DTLS handshake messages, SCTP user messages and TLS
records, based on the algorithms described in :rfc:`791`, :rfc:`815`, :rfc:`6347`,
:rfc:`9260`, :rfc:`8260` and :rfc:`9846`.

.. seealso::

Expand All @@ -21,6 +21,7 @@ based on the algorithms described in :rfc:`791`, :rfc:`815`, :rfc:`6347`,
tcp
dtls
sctp
tls

Every reassembly class subclasses
:class:`~pcapkit.foundation.reassembly.reassembly.ReassemblyBase`, reassembling
Expand All @@ -31,7 +32,7 @@ extracted fragments into whole datagrams:
flowchart TD
A{{ReassemblyMeta}} -.->|metaclass| B(ReassemblyBase)

B --> IP & TCP & DTLS & SCTP
B --> IP & TCP & DTLS & SCTP & TLS
IP --> IPv4 & IPv6

B --> C(Reassembly)
Expand All @@ -49,6 +50,7 @@ extracted fragments into whole datagrams:
click TCP "tcp.html#pcapkit.foundation.reassembly.tcp.reassembly.TCP"
click DTLS "dtls.html#pcapkit.foundation.reassembly.dtls.reassembly.DTLS"
click SCTP "sctp.html#pcapkit.foundation.reassembly.sctp.reassembly.SCTP"
click TLS "tls.html#pcapkit.foundation.reassembly.tls.reassembly.TLS"

Auxiliary Data
==============
Expand Down Expand Up @@ -83,7 +85,7 @@ What submits a buffer, and how its holes at that point pick the
flowchart TD
subgraph trig ["What submits a buffer"]
direction TB
T1["IP: every octet received<br/>TCP: a FIN and every octet below it"]
T1["IP: every octet received<br/>TCP, TLS: a FIN and every octet below it"]
T2["a session torn down: a TCP RST,<br/>a TCP SYN on a buffered 4-tuple,<br/>an unfragmented IPv4 datagram reusing the BUFID"]
T3["a TCP segment farther than TCP\_GAP\_LIMIT<br/>from its buffer: the stream splits"]
T4["a DTLS fragment whose length or type disagrees<br/>with the message held under its message\_seq"]
Expand All @@ -92,7 +94,7 @@ What submits a buffer, and how its holes at that point pick the
T7[".datagram or fetch() read outside a callback<br/>while buffers are outstanding"]
T1 ~~~ T2 ~~~ T3 ~~~ T4 ~~~ T5 ~~~ T6 ~~~ T7
end
T1D["DTLS, SCTP: a message whole"]
T1D["DTLS, SCTP: a message whole<br/>TLS: a record whole at the boundary"]
SUB["submit(buffer)"]
Q1{"overlap?"}
Q2{"no holes left?"}
Expand Down
2 changes: 1 addition & 1 deletion docs/source/pcapkit/foundation/reassembly/reassembly.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Reassembly Base Class
:class:`~pcapkit.foundation.reassembly.reassembly.Reassembly`, the abstract base
class of a custom reassembly class, and
:class:`~pcapkit.foundation.reassembly.reassembly.ReassemblyBase`, which it and
the built-in IP, TCP, DTLS and SCTP reassembly classes derive from. The algorithms
the built-in IP, TCP, DTLS, SCTP and TLS reassembly classes derive from. The algorithms
follow :rfc:`791` and :rfc:`815`.

.. autoclass:: pcapkit.foundation.reassembly.reassembly.Reassembly
Expand Down
162 changes: 162 additions & 0 deletions docs/source/pcapkit/foundation/reassembly/tls.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
=====================
TLS Record Reassembly
=====================

.. module:: pcapkit.foundation.reassembly.tls
.. module:: pcapkit.foundation.reassembly.tls.reassembly

:mod:`pcapkit.foundation.reassembly.tls.reassembly` contains
:class:`~pcapkit.foundation.reassembly.tls.reassembly.TLS` only, which reconstructs
the TLS records of a TCP stream, a record split across segments included
(:rfc:`9846#section-5`).

.. versionadded:: 1.9.0

.. autoclass:: pcapkit.foundation.reassembly.tls.reassembly.TLS
:no-members:
:show-inheritance:

.. rubric:: Defaults

.. autoattribute:: __protocol_name__
.. autoattribute:: __protocol_type__

.. rubric:: Methods

.. automethod:: reassembly
.. automethod:: submit

.. rubric:: Utilities

.. automethod:: _read_header
.. automethod:: _opens_record
.. automethod:: _close_runs
.. automethod:: _hold_segment
.. automethod:: _hold_octets
.. automethod:: _cut_records
.. automethod:: _stream_whole
.. automethod:: _make_datagram

Algorithm
=========

.. seealso::

A record over TCP is delimited by its header alone (:rfc:`9846#section-5.1`), and
the stream is the segments' octets in sequence order (:rfc:`9293#section-3.4`).

Each direction of a connection is a buffer, which holds the octets past the next
record's boundary as runs in sequence order. A segment's new octets join the runs,
and the records whole from the boundary on are cut off and reported:

.. mermaid::

flowchart TD
SEG["a segment, as TCP reassembly records it"]
SYN{"SYN?"}
OPEN["a fresh buffer, its boundary<br/>the octet after the SYN"]
HAS{"a buffer?"}
SYNC{"its payload opens<br/>a record header?"}
DROP["ignored"]
HOLD["hold the octets no earlier<br/>segment brought"]
HEAD{"a header held<br/>at the boundary?"}
BAD{"it opens a record?"}
GONE["the buffer is dropped"]
WHOLE{"the record whole?"}
CUT["report it COMPLETE, and<br/>move the boundary past it"]
END{"an RST, or a FIN with<br/>every octet before it?"}
CLOSE["submit the buffer, and<br/>close the stream"]
WAIT["wait for the next segment"]

SEG --> SYN
SYN -->|yes| OPEN --> HOLD
SYN -->|no| HAS
HAS -->|open| HOLD
HAS -->|closed| DROP
HAS -->|none| SYNC
SYNC -->|no| DROP
SYNC -->|"yes: a buffer from it"| HOLD
HOLD --> HEAD
HEAD -->|no| END
HEAD -->|yes| BAD
BAD -->|no| GONE
BAD -->|yes| WHOLE
WHOLE -->|yes| CUT --> HEAD
WHOLE -->|no| END
END -->|yes| CLOSE
END -->|no| WAIT

Without the SYN, the boundary a buffer opens at is a guess, which the length bound of
:meth:`TLS.record_header <pcapkit.protocols.application.tls.protocol.TLS.record_header>`
narrows but cannot make safe; the class's warning above says what it rules out.

:meth:`~pcapkit.foundation.reassembly.tls.reassembly.TLS.submit` reports what a buffer
still holds, record by record from the boundary: one whose fragment is whole
:attr:`~pcapkit.foundation.reassembly.data.Completion.COMPLETE`, and one with a hole
:attr:`~pcapkit.foundation.reassembly.data.Completion.PARTIAL`, or
:attr:`~pcapkit.foundation.reassembly.data.Completion.TIMEOUT` once expired. It stops at
the first hole a header lies in.

Terminology
===========

The reassembly is fed :term:`reasm.tcp.packet`, TCP reassembly's record of a segment.

.. glossary::

reasm.tls.datagram
Data structure for **reassembled TLS record** (element of
:attr:`TLS.datagram <pcapkit.foundation.reassembly.reassembly.Reassembly.datagram>`)
is :class:`~pcapkit.foundation.reassembly.tls.data.Datagram` -- ``completed``,
the original record identifier, the packet numbers that carried it, the
record header's ``content_type``, ``version`` and ``length``, ``fragment``,
the reassembled fragment (a contiguous :obj:`bytes`, or -- under
``strict=True`` and an incomplete record -- a :obj:`tuple` of the octet runs
actually received), and ``packet``, what
:func:`~pcapkit.foundation.analysis.tls.tls_record` decodes the record to.
``packet`` is decoded on first read, as a DTLS message's is, and is
:data:`None` for an incomplete record.

reasm.tls.buffer
Data structure for internal buffering
(:attr:`TLS._buffer <pcapkit.foundation.reassembly.reassembly.Reassembly._buffer>`)
is :class:`~pcapkit.foundation.reassembly.tls.data.Buffer` -- one direction of
a TCP connection, keyed by its
:data:`~pcapkit.foundation.reassembly.tls.data.BufferID`: the next record's
boundary, the octets held past it as
:class:`~pcapkit.foundation.reassembly.tls.data.Run` objects, the octets each
packet added, and where the stream ends.

Data Models
===========

.. module:: pcapkit.foundation.reassembly.tls.data

.. currentmodule:: pcapkit.foundation.reassembly.tls.reassembly

.. autodata:: pcapkit.foundation.reassembly.tls.data.BufferID

.. autoclass:: pcapkit.foundation.reassembly.tls.data.DatagramID
:members:
:show-inheritance:

.. autoclass:: pcapkit.foundation.reassembly.tls.data.Datagram
:members:
:show-inheritance:

.. autoclass:: pcapkit.foundation.reassembly.tls.data.Run
:members:
:show-inheritance:

.. autoclass:: pcapkit.foundation.reassembly.tls.data.Buffer
:members:
:show-inheritance:

Type Variables
==============

.. data:: _AT
:module: pcapkit.foundation.reassembly.tls.data
:type: ipaddress.IPv4Address | ipaddress.IPv6Address

IP address type.
5 changes: 3 additions & 2 deletions docs/source/pcapkit/protocols/application/dtls.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ exceptions:
:class:`~pcapkit.protocols.misc.raw.protocol.Raw` together with everything after it.
* Octets that open no whole record stay raw, with no error: an unsupported
content type, a legacy version outside ``0xFExx``, a header cut short, a
length past the end of the datagram, or a connection ID whose length is
not known, which leaves the record's end unknown.
length past the end of the datagram or past the RFCs' bound (``2 ** 14 +
2048``, or ``2 ** 14 + 256`` for a unified header), or a connection ID whose
length is not known, which leaves the record's end unknown.
* A parse told to stop at DTLS (``protocol='DTLS'`` or ``layer='Application'``)
keeps the rest of the datagram raw.
* Each record is a layer, so :data:`~pcapkit.utilities.config.FRAME_LAYER_LIMIT`
Expand Down
Loading
Loading