Skip to content

Commit

Permalink
Add support for Linux cooked capture v2, SLL2. (#569)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Walton <[email protected]>
  • Loading branch information
tjwalton and Thomas Walton authored May 14, 2021
1 parent 05f42f7 commit 276252a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions dpkt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
from . import sctp
from . import sip
from . import sll
from . import sll2
from . import smb
from . import ssl
from . import stp
Expand Down
3 changes: 2 additions & 1 deletion dpkt/pcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
DLT_ZWAVE_R3 = 262
DLT_WATTSTOPPER_DLM = 263
DLT_ISO_14443 = 264
DLT_LINUX_SLL2 = 276

if sys.platform.find('openbsd') != -1:
DLT_LOOP = 12
Expand All @@ -138,7 +139,7 @@

dltoff = {DLT_NULL: 4, DLT_EN10MB: 14, DLT_IEEE802: 22, DLT_ARCNET: 6,
DLT_SLIP: 16, DLT_PPP: 4, DLT_FDDI: 21, DLT_PFLOG: 48, DLT_PFSYNC: 4,
DLT_LOOP: 4, DLT_LINUX_SLL: 16}
DLT_LOOP: 4, DLT_LINUX_SLL: 16, DLT_LINUX_SLL2: 20}


class PktHdr(dpkt.Packet):
Expand Down
54 changes: 54 additions & 0 deletions dpkt/sll2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""Linux libpcap "cooked v2" capture encapsulation."""
from __future__ import absolute_import

from . import arp
from . import dpkt
from . import ethernet


class SLL2(dpkt.Packet):
"""Linux libpcap "cooked v2" capture encapsulation.
See https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html
Attributes:
__hdr__: Header fields of SLLv2.
"""

__hdr__ = (
('ethtype', 'H', ethernet.ETH_TYPE_IP),
('mbz', 'H', 0), # reserved
('intindex', 'i', 0), # the 1-based index of the interface on which the packet was observed
('hrd', 'H', arp.ARP_HRD_ETH),
('type', 'B', 0), # 0: to us, 1: bcast, 2: mcast, 3: other, 4: from us
('hlen', 'B', 6), # hardware address length
('hdr', '8s', b''), # first 8 bytes of link-layer header
)
_typesw = ethernet.Ethernet._typesw

def unpack(self, buf):
dpkt.Packet.unpack(self, buf)
try:
self.data = self._typesw[self.ethtype](self.data)
setattr(self, self.data.__class__.__name__.lower(), self.data)
except (KeyError, dpkt.UnpackError):
pass


def test_sll2():
sll2data = (b'\x08\x00\x00\x00\x00\x00\x00\x03\x00\x01\x00\x06\x00\x0b\xdb\x52\x0e\x08\xf6\x7f'
b'\x45\x00\x00\x34\xcc\x6c\x40\x00\x40\x06\x74\x08\x82\xd9\xfa\x8e\x82\xd9\xfa\x0d')
sll2test = SLL2(sll2data)
assert sll2test.type == 0
assert sll2test.mbz == 0
assert sll2test.intindex == 3
assert sll2test.hrd == 1
assert sll2test.hlen == 6
assert sll2test.hdr == b'\x00\x0b\xdb\x52\x0e\x08\xf6\x7f'
assert sll2test.ethtype == 0x0800

# give invalid ethtype of 0x1234 to make sure error is handled
sll2data2 = (b'\x12\x34\x00\x00\x00\x00\x00\x03\x00\x01\x00\x06\x00\x0b\xdb\x52\x0e\x08\xf6\x7f'
b'\x45\x00\x00\x34\xcc\x6c\x40\x00\x40\x06\x74\x08\x82\xd9\xfa\x8e\x82\xd9\xfa\x0d')
sll2test2 = SLL2(sll2data2)

0 comments on commit 276252a

Please sign in to comment.