STP - Spanning Tree Protocol

pcapkit.protocols.application.stp.protocol holds STP, the abstract base of the Spanning Tree Protocol family [*], whose Bridge Protocol Data Units (BPDUs) are told apart by their Protocol Version Identifier. Each version is a member of its own:

Version

Class

Specification

BPDUs

0

STPv0

IEEE Std 802.1D-2004 clause 9

Configuration, 35 octets, and Topology Change Notification (TCN), 4 octets

2

RSTP

IEEE Std 802.1Q-2022 clause 14

RST, 36 octets

3

MSTP

IEEE Std 802.1Q-2022 clause 14

MST, 102 octets and an MSTI Configuration Message of 16 for each MSTI

The members share the first four octets alone, the Protocol Identifier, the Protocol Version Identifier and the BPDU Type, so the base parses no BPDU. LLC address 0x42 is the family’s, as DSAP and SSAP both, so LLC.__proto__ binds this base, paired, and it picks the member as IEEE Std 802.1Q-2022 clause 14.4 validates a BPDU, by _classify(). A BPDU that clause discards is left Raw behind LLC, and a direct parse of it raises ProtocolError; so is an SPT BPDU, of version 4, which the family does not dissect.

The bridge’s control plane computes the active topology rather than relaying frames, so the family is a user of the stack, as a routing protocol is (Protocol Layer Placement). The proprietary per-VLAN variants, which no public specification defines, are not dissected.

Added in version 1.9.0.

class pcapkit.protocols.application.stp.protocol.STP[_PT: Data, _ST: Schema](file, length=None, /, *, version=None, layer=None, protocol=None, context=None)[source]

Bases: Application[_PT, _ST], Generic[_PT, _ST]

This class implements all protocols in the Spanning Tree Protocol family.

It is abstract, as OSPF is, and dispatches on both paths, never giving an instance of this class (#2574). The version keyword names the member, and make() does not take it; without it, octets give the member _classify() picks, and fields are refused. The Protocol Version Identifier alone does not pick it: a Configuration BPDU is one whatever its version, and an MST BPDU that fails its checks is an RST BPDU, so from_data() reads the data model’s class, as LinuxSLL.from_data does. See __new__().

Note

The subpackage does not track the dispatch tier. STP is dispatched from LLC.__proto__ at Bridge_Spanning_Tree_Protocol, since LLC is what carries it.

Properties

abstract property version: Literal[0, 2, 3]

Version of the member, the Protocol Version Identifier its BPDUs take.

property alias: str

Acronym of current protocol, 'STP', 'RSTP' or 'MSTP'.

property info_name: Literal['stp']

Key name of the info dict.

property type: BPDUType

BPDU Type.

Methods

classmethod id()[source]

Index ID of the protocol.

IEEE Std 802.1D-2004 names the version 0 protocol STP, without a version, so the family’s own name doubles as its canonical name, the shape IGMP.id takes.

Return type:

tuple[Literal['STP'], Literal['RSTP'], Literal['MSTP']]

Returns:

Index ID of the protocol, the family name and the RSTP and MSTP names; each member narrows it to its own canonical name.

classmethod from_data(data, /, **kwargs)[source]

Create protocol instance from data.

Parameters:
Return type:

Self

Returns:

Protocol instance; on this class, an instance of the member whose data model data is.

Raises:

ProtocolError – If data is no member’s data model, on this class. A dict is refused: its protocol_version does not pick the member, and no key does.

Data Models

classmethod __index__()[source]

Numeral registry index of the protocol.

Return type:

LSAP

Returns:

Numeral registry index of the protocol in the LLC address values, which every member inherits, since no link type, EtherType, protocol number or port names it.

__length_hint__()[source]

Return an estimated length for the object.

The longest member’s fixed part, an MST BPDU with no MSTI, as OSPF.__length_hint__ takes its longest header.

Return type:

int

__init__(file=NO_VALUE, length=None, /, *, version=None, layer=None, protocol=None, context=None, **fields)[source]

Parse a protocol instance from its octets, or construct one from its fields.

Overloads:
Parameters:
Raises:

ProtocolError – If version names no member, or is not this class’s own version.

static __new__(cls, file=NO_VALUE, length=None, /, **kwargs)[source]

Create the member a parse or a construction of this class describes.

The version keyword names the member. Without it, the octets give the member _classify() picks, and fields are refused. That member’s __init__ then runs with the same arguments. A subclass of this class is created as itself.

Parameters:
  • file (IO[bytes] | bytes | None | NoValueType) – Source packet stream, read here without being consumed; omitted to construct.

  • length (int | None) – Length of packet data.

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

Return type:

STP

Returns:

An uninitialised instance of the member.

Raises:

Utilities

static _member_of_version(version)[source]

The member a version of 0, 2 or 3 names.

Parameters:

version (Any) – Version of the member, as _classify() returns it or the version keyword gives it.

Return type:

Type[STP]

Returns:

STPv0 for 0, RSTP for 2 and MSTP for 3.

Raises:

ProtocolError – For any other version, or one that is no int, as a bool is even where it equals one.

classmethod _classify(data)[source]

Validate a received BPDU as IEEE Std 802.1Q-2022 clause 14.4 does.

With a Protocol Identifier of zero, a BPDU Type of 0x00 and 35 octets or more is a Configuration BPDU, and one of 0x80 and 4 octets or more a TCN BPDU, whatever the version; with a BPDU Type of 0x02, a version of 2 and 36 octets or more is an RST BPDU. A version of 3 or more, with 35 octets or more, is an RST BPDU where _is_mst() fails; where it holds, version 3 is an MST BPDU, and a later version the SPT BPDU of Shortest Path Bridging, version 4, which extends it and which no member reads. Anything else is discarded.

Parameters:

data (bytes) – The BPDU, its octets alone.

Return type:

Literal[-1, 0, 2, 3]

Returns:

0, 2 or 3, the version of the member that reads it, or -1 for a BPDU the clause discards or no member reads.

static _is_mst(data)[source]

Whether a BPDU of version 3 or more holds what an MST BPDU does.

It holds 102 octets or more, a Version 1 Length of zero, and a Version 3 Length counting 0 to 64 MSTI Configuration Messages of 16 octets each after the 64 octets every MST BPDU has there (IEEE Std 802.1Q-2022 clause 14.4).

Parameters:

data (bytes) – The BPDU, its octets alone.

Return type:

bool

Returns:

Whether it is an MST BPDU, rather than an RST BPDU.

classmethod _construction_member(fields)[source]

The member from_fields(), from_data() and a call given no file construct.

On this class, the member version names; on another, the class itself, whose version version must be. version is taken out of fields, as make() does not read it.

Parameters:

fields (dict[str, Any]) – Construction keywords.

Return type:

Type[Self]

Returns:

The member, or the class itself.

Raises:

ProtocolError – If, on this class, version is missing; if it names no member; or if it is not the class’s own version.

static _rebuilt_version(data, own)[source]

The version a rebuild of data writes: its own, or own if the capture ended first.

A capture that ends before the Protocol Version Identifier leaves it zero, which names no RST or MST BPDU’s version; the rebuild is cut back before the octet, so the version it writes is never read, as for OSPF._rebuilt_version.

Parameters:
  • data (Any) – The data model being rebuilt.

  • own (int) – The version of the member rebuilding it.

Return type:

int

Returns:

The version for make().

_check_bpdu(schema, *, types, least=0, most=0xFF)[source]

Refuse a BPDU another member reads, or one no member does.

Its Protocol Identifier must be zero, its BPDU Type one of types and its Protocol Version Identifier from least to most. A field a header parsed on its own ends before is left to the short read, as its zeros are the cut’s.

Parameters:
  • schema (Schema) – The header just parsed.

  • types (tuple[int, ...]) – The BPDU Types this member reads.

  • least (int) – The lowest Protocol Version Identifier it reads.

  • most (int) – The highest.

Raises:

ProtocolError – If one of them is not, and was read.

_read_bridge_id(octets)[source]

Read a Bridge Identifier.

Parameters:

octets (bytes) – Its eight octets; fewer where a cut ended them, the rest read as zeros.

Return type:

BridgeID

Returns:

Parsed Bridge Identifier.

_make_bridge_id(value, *, name)[source]

Make a Bridge Identifier.

Parameters:
  • value (BridgeID | bytes) – The identifier, as its data model or a mapping of its keys, or its eight octets.

  • name (str) – Name of the field, for the error message.

Return type:

bytes

Returns:

Its eight octets.

Raises:

ProtocolError – If value is neither; if its priority is not a multiple of 4096 in 0..61440; or if its extension does not fit in 12 bits, or its address is no MAC address.

static _read_port_id(value)[source]

Read a Port Identifier.

Parameters:

value (int) – Its two octets, as an integer.

Return type:

PortID

Returns:

Parsed Port Identifier.

_make_port_id(value, *, name)[source]

Make a Port Identifier.

Parameters:
  • value (PortID | int) – The identifier, as its data model or a mapping of its keys, or its two octets as an integer.

  • name (str) – Name of the field, for the error message.

Return type:

int

Returns:

Its two octets, as an integer.

Raises:

ProtocolError – If value is neither; if its priority is not a multiple of 16 in 0..240; or if its number does not fit in 12 bits.

static _read_flags(value)[source]

Read the flags of an RST or MST BPDU.

Parameters:

value (int) – The flags octet.

Return type:

Flags

Returns:

Parsed flags.

_make_flags(value)[source]

Make the flags of an RST or MST BPDU.

Parameters:

value (Flags | int) – The flags, as their data model or a mapping of its keys, or the octet as an integer.

Return type:

int

Returns:

The flags octet.

Raises:

ProtocolError – If value is neither, or a flag is no bool or the role does not fit in two bits.

_make_role_flags(value, *, last, name)[source]

Make a flags octet carrying a Port Role, the CIST’s or an MSTI’s.

Parameters:
  • value (Mapping[str, Any]) – The flags.

  • last (str) – Key of the flag in bit 8.

  • name (str) – Name of the flag in bit 8, for the error message.

Return type:

int

Returns:

The flags octet.

Raises:

ProtocolError – If a flag is no bool, or the role does not fit in two bits.

static _read_timer(value)[source]

Read a timer, in units of 1/256 of a second (IEEE Std 802.1D-2004 clause 9).

Parameters:

value (int) – Its two octets, as an integer.

Return type:

float

Returns:

The timer, in seconds, exact as 256 is a power of two.

_make_timer(value, *, name)[source]

Make a timer, in units of 1/256 of a second.

Parameters:
  • value (float | int) – The timer, in seconds.

  • name (str) – Name of the timer, for the error message.

Return type:

int

Returns:

Its two octets, as an integer.

Raises:

ProtocolError – If value is no number of seconds, a bool included, is not a multiple of 1/256, or does not fit in 16 bits.

_make_protocol_version(value, *, least, most=0xFF)[source]

Check a Protocol Version Identifier this member writes.

Parameters:
  • value (ProtocolVersion | int) – The Protocol Version Identifier.

  • least (int) – The lowest version this member’s BPDUs take.

  • most (int) – The highest.

Return type:

int

Returns:

value, as a plain int.

Raises:

ProtocolError – If value is no 8-bit code, or is outside least to most.

Data Models

class pcapkit.protocols.application.stp.data.BridgeID(priority, extension, address)[source]

Bases: Data

Data model for a Bridge Identifier, eight octets (IEEE Std 802.1D-2004 clause 9).

priority: int

Bridge Priority, the four most significant bits, a multiple of 4096.

extension: int

System ID Extension, the next 12 bits; the MSTID in an MSTI Regional Root Identifier.

address: str

MAC address, the last six octets.

class pcapkit.protocols.application.stp.data.PortID(priority, number)[source]

Bases: Data

Data model for a Port Identifier, two octets (IEEE Std 802.1D-2004 clause 9).

priority: int

Port Priority, the four most significant bits, a multiple of 16.

number: int

Port Number, the remaining 12 bits.

class pcapkit.protocols.application.stp.data.Flags(tc, proposal, role, learning, forwarding, agreement, tc_ack)[source]

Bases: Data

Data model for the flags of an RST or MST BPDU, octet 5.

Bit 1 is the least significant bit of the octet.

tc: bool

Topology Change flag, bit 1.

proposal: bool

Proposal flag, bit 2.

role: PortRole

Port Role, bits 3 and 4; its 0, a Master Port in the MSTI Flags, is Unknown here (IEEE Std 802.1D-2004).

learning: bool

Learning flag, bit 5.

forwarding: bool

Forwarding flag, bit 6.

agreement: bool

Agreement flag, bit 7.

tc_ack: bool

Topology Change Acknowledgment flag, bit 8.

Footnotes