@@ -334,6 +334,67 @@ def _ipv4_accepted(ipv4: 'IP') -> 'bool':
334334 return _default_accepts ('IPv4' , octets [:max ((octets [0 ] & 0x0F ) * 4 , 20 )])
335335
336336
337+ def _quoted (layer : 'Packet' ) -> 'bool' :
338+ """Test if a Scapy layer heads the datagram an ICMP or ICMPv6 error quotes.
339+
340+ Args:
341+ layer: A layer of a dissected Scapy packet.
342+
343+ Returns:
344+ :data:`True` if ``layer`` is a :class:`~scapy.layers.inet.IPerror` or
345+ :class:`~scapy.layers.inet6.IPerror6`; else :data:`False`.
346+
347+ Note:
348+ The default engine has no ICMP or ICMPv6 parser, so it dissects nothing
349+ an ICMP message carries. `Scapy`_ dissects the datagram an error quotes
350+ (:rfc:`792`, :rfc:`4443#section-3`) as ``IPerror`` or ``IPerror6``,
351+ subclasses of :class:`~scapy.layers.inet.IP` and
352+ :class:`~scapy.layers.inet6.IPv6`, and those bind the full layers of an
353+ IP-in-IP tunnel: ``IPerror`` with protocol 41 binds an ``IPv6``, and
354+ ``IPerror6`` with next header 4 or 41 an ``IP`` or an ``IPv6``, each of
355+ which carries a real ``TCP`` (:issue:`1627`). Everything from such a
356+ layer on is out of the default engine's reach.
357+
358+ """
359+ from scapy .layers .inet import IPerror
360+ from scapy .layers .inet6 import IPerror6
361+
362+ return isinstance (layer , (IPerror , IPerror6 ))
363+
364+
365+ def _first_ipv4 (packet : 'Packet' ) -> 'IP | None' :
366+ """Fetch the first IPv4 layer of a Scapy packet, as the default engine dissects it.
367+
368+ Args:
369+ packet: Scapy packet.
370+
371+ Returns:
372+ The first :class:`~scapy.layers.inet.IP` layer of the packet, however
373+ deep -- e.g. one carried in IPv6 (:rfc:`2473`) -- or :data:`None` if
374+ there is none before the datagram an ICMP or ICMPv6 error quotes (see
375+ :func:`_quoted`).
376+
377+ Note:
378+ The default engine takes ``frame['IPv4']``
379+ (:func:`pcapkit.toolkit.pcap.ipv4_reassembly`). ``packet['IP']`` finds
380+ the same layer but for one inside an ICMPv6 error's quote, which it
381+ reaches through the ``IPerror6`` there, and ``getlayer()`` searches the
382+ packets a field holds as well, such as the IPv6 packet a Redirected
383+ Header option carries (:rfc:`4861#section-4.6.3`), so the payloads are
384+ walked instead.
385+
386+ """
387+ from scapy .layers .inet import IP
388+ from scapy .packet import NoPayload
389+
390+ layer = packet # type: Packet
391+ while not isinstance (layer , NoPayload ) and not _quoted (layer ):
392+ if isinstance (layer , IP ):
393+ return layer
394+ layer = layer .payload
395+ return None
396+
397+
337398def _tcp_behind_extension_headers (segment : 'bytes' ) -> 'tuple[bytes, bytes] | None' :
338399 """Split a TCP segment the default engine reads behind an AH or IPv6 extension header in IPv4.
339400
@@ -379,7 +440,9 @@ def _tcp_carrier(packet: 'Packet') -> 'tuple[IP | IPv6, TCP, bytes, bytes] | Non
379440 the default engine reads no TCP there: an IPv4 layer above it has a
380441 header that engine rejects (see :func:`_ipv4_accepted`), or is a later
381442 fragment, whose data is a slice of the datagram's payload with no
382- upper-layer header in it (:rfc:`791#section-3.2`, :issue:`1617`).
443+ upper-layer header in it (:rfc:`791#section-3.2`, :issue:`1617`), or
444+ the TCP layer lies in the datagram an ICMP or ICMPv6 error quotes (see
445+ :func:`_quoted`, :issue:`1627`).
383446
384447 An IPv4 layer whose protocol names an AH or IPv6 extension header
385448 carries what the default engine reads past those headers, by its own
@@ -408,22 +471,21 @@ def _tcp_carrier(packet: 'Packet') -> 'tuple[IP | IPv6, TCP, bytes, bytes] | Non
408471 offset as it binds TCP.
409472
410473 """
411- from scapy .layers .inet import IP , TCP , IPerror
474+ from scapy .layers .inet import IP , TCP
412475 from scapy .layers .inet6 import IPv6
413476 from scapy .packet import NoPayload
414477
415478 tcp = cast ('TCP | None' , packet .getlayer ('TCP' ))
416479 carrier = None # type: IP | IPv6 | None
417480 layer = packet # type: Packet
418481 while layer is not tcp :
419- if isinstance (layer , NoPayload ):
482+ if isinstance (layer , NoPayload ) or _quoted ( layer ) :
420483 return None
421484 if isinstance (layer , IP ):
422485 if not _ipv4_accepted (layer ) or layer .frag :
423486 return None
424487 carrier = layer
425- # not the datagram an ICMP error quotes, which the default engine does not dissect
426- if layer .proto in IPV4_EXTENSION_HEADERS and not isinstance (layer , IPerror ):
488+ if layer .proto in IPV4_EXTENSION_HEADERS :
427489 upper = _past_extension_headers (layer .proto , _within_datagrams (layer .payload ))
428490 if upper is not None and upper [0 ] == Enum_TransType .TCP :
429491 segment = _tcp_behind_extension_headers (upper [1 ])
@@ -456,8 +518,11 @@ def ipv4_reassembly(packet: 'Packet', *, count: 'int' = -1) -> 'IP_Packet[IPv4Ad
456518 Data for IPv4 reassembly.
457519
458520 * If the ``packet`` can be used for IPv4 reassembly. A packet can be reassembled
459- if it contains IPv4 layer (:class:`scapy.layers.inet.IP`) whose header the
460- default engine accepts (see :func:`_ipv4_accepted`), and the **DF**
521+ if it contains IPv4 layer (:class:`scapy.layers.inet.IP`) -- the first of the
522+ frame, as for the default engine, so one carried in IPv6 counts but none an
523+ ICMP or ICMPv6 error quotes (see :func:`_first_ipv4`) -- whose header the
524+ default engine accepts (see :func:`_ipv4_accepted`) and lies inside every
525+ IPv6 packet carrying it, and the **DF**
461526 (:attr:`scapy.layers.inet.IP.flags.DF`) flag is :data:`False`.
462527 * If the ``packet`` can be reassembled, then the :obj:`dict` mapping of data for IPv4
463528 reassembly (:term:`reasm.ipv4.packet`) will be returned; otherwise, returns :data:`None`.
@@ -466,11 +531,20 @@ def ipv4_reassembly(packet: 'Packet', *, count: 'int' = -1) -> 'IP_Packet[IPv4Ad
466531 :class:`pcapkit.foundation.reassembly.ipv4.IPv4`
467532
468533 """
469- if 'IP' in packet :
470- ipv4 = cast ('IP' , packet ['IP' ])
534+ if scapy is None :
535+ raise ModuleNotFound ("No module named 'scapy'" , name = 'scapy' )
536+
537+ if (ipv4 := _first_ipv4 (packet )) is not None :
471538 if ipv4 .flags .DF : # dismiss not fragmented packet
472539 return None
473- if not _ipv4_accepted (ipv4 ):
540+ # NOTE: ``bytes(ipv4)`` ends with the frame's trailer, which Scapy files
541+ # as padding at the end of the packet however deep the IPv4 layer, but
542+ # the default engine hands it only the octets inside the Payload Length
543+ # of each IPv6 packet above it, so those are all it keeps (:issue:`1623`)
544+ octets = _within_datagrams (ipv4 )
545+ ihl = ipv4 .ihl * 4
546+ # the default engine dissects no IPv4 header those octets cut short
547+ if len (octets ) < ihl or not _ipv4_accepted (ipv4 ):
474548 return None
475549
476550 data = IP_Packet (
@@ -489,15 +563,15 @@ def ipv4_reassembly(packet: 'Packet', *, count: 'int' = -1) -> 'IP_Packet[IPv4Ad
489563 # same scaling this module's own IPv6 path already applies to
490564 # ``IPv6ExtHdrFragment.offset`` below.
491565 fo = ipv4 .frag * 8 , # fragment offset
492- ihl = ipv4 . ihl * 4 , # internet header length
566+ ihl = ihl , # internet header length
493567 mf = bool (ipv4 .flags .MF ), # more fragment flag
494568 tl = ipv4 .len , # total length, header includes
495- header = bytes ( ipv4 )[: ipv4 . ihl * 4 ], # raw bytes type header
569+ header = octets [: ihl ], # raw bytes type header
496570 # NOTE: this keeps the octets past the Total Length, as the default
497571 # engine's adapter does, since the reassembly machinery cuts the
498572 # payload at ``tl - ihl`` itself. :func:`ipv6_reassembly` derives
499573 # its ``tl`` from the payload, so it has to cut them (:issue:`1584`).
500- payload = bytearray (bytes ( ipv4 . payload )), # raw bytearray type payload
574+ payload = bytearray (octets [ ihl :]), # raw bytearray type payload
501575 timestamp = float (packet .time ), # capture timestamp
502576 )
503577 return data
@@ -588,7 +662,8 @@ def tcp_reassembly(packet: 'Packet', *, count: 'int' = -1) -> 'TCP_Packet | None
588662 * If the ``packet`` can be used for TCP reassembly. A packet can be reassembled
589663 if it contains TCP layer (:class:`scapy.layers.inet.TCP`) whose fixed header
590664 lies inside its datagram, and whose Data Offset counts at least that header,
591- behind no IPv4 header the default engine rejects and in no later IPv4 fragment,
665+ behind no IPv4 header the default engine rejects, in no later IPv4 fragment and
666+ in no datagram an ICMP or ICMPv6 error quotes,
592667 or a TCP segment the default engine reads behind an AH or IPv6 extension header
593668 in IPv4. It is keyed by the IP layer that carries it -- in an IP-in-IP tunnel,
594669 the innermost (see :func:`_tcp_carrier`).
0 commit comments