Ethernet Protocol

Removed in version 1.6.0: pcapkit.protocols.link.ethernet.PAT_MAC_ADDR, with no alias: _read_mac_addr() and _make_mac_addr() read and make the address (#2614).

pcapkit.protocols.link.ethernet.protocol contains Ethernet only, which implements the extractor for Ethernet Protocol [*], whose structure is:

Octets

Bits

Name

Description

0

0

ethernet.dst

Destination MAC Address

6

48

ethernet.src

Source MAC Address

12

96

ethernet.type

Length/Type

The Length/Type field is an EtherType from 0x0600 up. Up to 0x05DC it is the length of an IEEE 802.2 LLC PDU, and the octets past it are kept as ethernet.trailer (IEEE Std 802.3-2022 §3.2.6).

class pcapkit.protocols.link.ethernet.protocol.Ethernet(file, length=None, /, *, layer=None, protocol=None, context=None)[source]

Bases: Link[Ethernet, Ethernet]

This class implements Ethernet Protocol.

The Length/Type field is an EtherType from 0x0600 up, and the payload is dispatched through Link.__proto__. Up to 0x05DC it is the length of the MAC client data, an IEEE 802.2 LLC PDU, and the octets past it, the pad among them, are kept as trailer (IEEE Std 802.3-2022 §3.2.6). A value between the two is neither, and its payload is kept as Raw.

Changed in version 1.9.0: A Length/Type field up to 0x05DC is a length, and the payload it bounds is dissected as LLC, where it was kept raw; make() takes the trailer (#2936).

Changed in version 1.6.0: type of make() takes a member or an int code, resolved with the enumeration’s get; a name, a bool, or a code the field cannot carry raises ProtocolError, rather than resolving against an enumeration inferred from the caller’s frame or writing a different value (#2594).

Removed in version 1.6.0: The type_default, type_namespace and type_reversed arguments of make(); resolve a name with resolve_index() first (#2594).

Properties

property name: Literal['Ethernet Protocol']

Name of current protocol.

property length: Literal[14]

Header length of current protocol.

property protocol: EtherType

Name of next layer protocol, the Length/Type field.

property src: str

Source mac address.

property dst: str

Destination mac address.

Methods

read(length=None, **kwargs)[source]

Read Ethernet Protocol.

Structure of Ethernet header [RFC 7042]:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                         Dst MAC Addr                          |
+                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                               |                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               +
|                         Src MAC Addr                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Ether Type           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Parameters:
  • length (int | None) – Length of packet data.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type:

Ethernet

Returns:

Parsed packet data.

make(*, dst='00:00:00:00:00:00', src='00:00:00:00:00:00', type=EtherType.Internet_Protocol_version_4, payload=b'', trailer=b'', **kwargs)[source]

Make (construct) packet data.

Parameters:
  • dst (str | bytes | bytearray) – Destination MAC address.

  • src (str | bytes | bytearray) – Source MAC address.

  • type (EtherType | int) – Length/Type: an EtherType, or, up to 0x05DC, the length of the payload, which it has to be but in a rebuild of a cut frame.

  • payload (bytes | ProtocolBase | Schema) – Payload data.

  • trailer (bytes) – Octets after the payload a length gives, such as the pad; written as is.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type:

Ethernet

Returns:

Constructed packet data.

Raises:

ProtocolError – If dst or src is rejected by _make_mac_addr(); if type is a name, a bool, or a member or code its field cannot carry; if a length is not that of payload; or if trailer is given with no length to end the payload.

Data Models

classmethod __index__()[source]

Numeral registry index of the protocol.

Return type:

LinkType

Returns:

Numeral registry index of the protocol in tcpdump link-layer header types.

Utilities

classmethod _make_data(data)[source]

Create key-value pairs from data for protocol construction.

Parameters:

data (Ethernet) – protocol data

Return type:

dict[str, Any]

Returns:

Key-value pairs for protocol construction.

_import_next_layer(proto, length=None, *, packet=None)[source]

Import next layer extractor.

Parameters:
  • proto (int) – next layer protocol index, the Length/Type field

  • length (int | None) – valid (non-padding) length

  • packet (dict[str, Any] | None) – packet info (passed from self.unpack)

Returns:

LLC where proto is a length, and otherwise the class Link.__proto__ registers for it.

Return type:

ProtocolBase

Header Schemas

Removed in version 1.6.0: pcapkit.protocols.schema.link.ethernet, with no alias: the module moved here (#2534).

class pcapkit.protocols.link.ethernet.schema.Ethernet(dst=NO_VALUE, src=NO_VALUE, type=NO_VALUE, payload=NO_VALUE, trailer=NO_VALUE, *, __packet__=None, **kwargs)[source]

Bases: Schema

Header schema for ethernet packet.

dst: bytes = <BytesField dst>

Destination MAC address.

src: bytes = <BytesField src>

Source MAC address.

type: EtherType = <EnumField type>

Length/Type, an EtherType or the length of the payload.

payload: bytes = <PayloadField payload>

Payload.

trailer: bytes = <PayloadField trailer>

Octets captured past the length a Length/Type field gives, the pad among them.

Auxiliary Functions

pcapkit.protocols.link.ethernet.schema.callback_payload(self, packet)[source]

Callback function for Ethernet.payload.

Parameters:
  • self (PayloadField) – Payload field to resolve.

  • packet (dict[str, Any]) – Packet data, whose type names the next layer.

Returns:

LLC where type is a length, and the class registered for it otherwise.

Return type:

None; the resolved class is assigned to self.protocol

Important

The lookup goes through Protocol._lookup_next_layer rather than subscripting the registry. Ethernet.__proto__ is Link.__proto__, a class-level collections.defaultdict, so subscripting it with a missing key would insert that key – every unregistered EtherType a parse saw would be recorded as though somebody had registered it, and Link.register would afterwards report it as an overwrite. The helper reads the fallback without recording it, and resolves a ModuleDescriptor without writing the class back, so the registry is left as registered (#1559).

pcapkit.protocols.link.ethernet.schema.ethernet_payload_length(pkt)[source]

Length of the Ethernet.payload field.

Parameters:

pkt (dict[str, Any]) – Packet data.

Return type:

int

Returns:

The Length/Type field where it is a length (IEEE Std 802.3-2022 §3.2.6), so that the pad after the MAC client data is Ethernet.trailer; the rest of the frame otherwise.

Data Models

Removed in version 1.6.0: pcapkit.protocols.data.link.ethernet, with no alias: the module moved here (#2534).

class pcapkit.protocols.link.ethernet.data.Ethernet(dst, src, type)[source]

Bases: Protocol

Data model for ethernet packet.

dst: str

Destination MAC address.

src: str

Source MAC address.

type: EtherType

Length/Type, an EtherType or the length of the payload.

trailer: bytes

Octets captured past the length a Length/Type field gives, the pad among them, present only when there are any.

Constants

Added in version 1.9.0.

pcapkit.protocols.link.ethernet.const.LENGTH_MAX

Largest value of the Length/Type field that is a length, the number of octets of MAC client data, rather than an EtherType (IEEE Std 802.3-2022 §3.2.6). An EtherType is 0x0600 or more, and the values between are neither.

Footnotes