packet

package
v0.0.0-...-6b8ee43 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package packet is the OSPFv3 (RFC 5340) packet and LSA wire codec: the protocol's serialization boundary. It parses received OSPFv3 payloads (raw IPv6 proto-89 datagrams, after the IPv6 header has been stripped) into packet and LSA views, and serializes packet/LSA structs back to bytes.

Layering (plan/spec-ospfv3-0-umbrella.md): types (leaf) <- packet <- the OSPFv3 runtime (transport, interface ISM, neighbor NSM, LSDB, SPF). This package imports only internal/plugins/ospf/v3/types plus the Go standard library; it shares NO code with the OSPFv2 codec. The OSPFv3 wire contract differs enough that sharing would leak the divergences:

  • a 16-octet common header with an Instance ID and no AuType/Authentication field (the OSPFv2 24-octet header has neither),
  • an IPv6 upper-layer (pseudo-header) packet checksum that binds the IPv6 source and destination, not the OSPFv2 over-the-packet Internet checksum,
  • a 24-bit Options field,
  • a 20-octet LSA header with a 16-bit LS Type and no Options byte,
  • address-free Router-LSAs and Network-LSAs (IPv6 addresses live in the Link-LSA and Intra-Area-Prefix-LSA),
  • and the RFC 5340 IPv6 prefix encoding.

Decode is zero-copy: decoded packets and LSAs retain slices owned by the caller, and LSA body parsing is available on demand. A decoded view is valid only while the caller's backing slice remains stable; LSDB code copies retained LSA bytes before storing.

Encode is buffer-first: WriteTo(buf, off) methods write into a caller-provided buffer, skip the Packet Length / Checksum and LSA Length / Checksum fields, and backfill them last. Unknown LS Types retain and re-emit their raw spans verbatim so flooding never re-marshals a received LSA.

Index

Constants

View Source
const (
	DDFlagInit   = 0x04 // I-bit: this is the first DD in the sequence
	DDFlagMore   = 0x02 // M-bit: more DD packets follow
	DDFlagMaster = 0x01 // MS-bit: the sender is the master
)

Database Description flags (RFC 5340 §A.3.3), unchanged from OSPFv2.

View Source
const (
	// Version is the OSPFv3 version number (RFC 5340 §A.3.1).
	Version = 3
	// CommonHeaderLen is the fixed OSPFv3 common header width in octets. RFC 5340
	// §A.3.1 removed the OSPFv2 8-octet AuType + Authentication field; the two
	// reclaimed octets are Instance ID + Reserved.
	CommonHeaderLen = 16
	// MaxPacketLen bounds Packet Length to the 16-bit field width.
	MaxPacketLen = 65535
)
View Source
const (
	ExternalFlagE = 0x04 // E-bit: Type 2 external metric (not comparable to OSPF cost)
	ExternalFlagF = 0x02 // F-bit: a Forwarding Address is present
	ExternalFlagT = 0x01 // T-bit: an External Route Tag is present
)

AS-External / NSSA flag bits (RFC 5340 §A.4.7). They occupy the low three bits of the first body octet, which shares the leading 32-bit word with the 24-bit Metric: byte 0 = "00000|E|F|T", bytes 1-3 = Metric.

View Source
const (
	// GraceTLVPeriod is the RFC 5187 §2.2 Grace Period tlv (Type 1, Length 4): the seconds
	// neighbors keep advertising the router as fully adjacent.
	GraceTLVPeriod uint16 = 1
	// GraceTLVReason is the RFC 5187 §2.2 Graceful Restart Reason tlv (Type 2, Length 1):
	// 0 unknown, 1 software restart, 2 software reload/upgrade, 3 switch to redundant CP.
	GraceTLVReason uint16 = 2
)
View Source
const (
	RouterFlagB  = 0x01 // B-bit: area border router
	RouterFlagE  = 0x02 // E-bit: AS boundary router
	RouterFlagV  = 0x04 // V-bit: virtual link endpoint
	RouterFlagNt = 0x10 // Nt-bit: NSSA border router translating Type-7 to Type-5
)

Router-LSA flag bits (RFC 5340 §A.4.3).

View Source
const (
	RouterLinkTypeP2P     = 1 // point-to-point connection to another router
	RouterLinkTypeTransit = 2 // connection to a transit network
	RouterLinkTypeVirtual = 4 // virtual link
)

Router-LSA link record types (RFC 5340 §A.4.3).

View Source
const (

	// LSAHeaderLen is the fixed OSPFv3 LSA header width in octets.
	LSAHeaderLen = 20
)

LSA header field offsets (RFC 5340 §A.4.2.1). There is NO Options byte in the OSPFv3 LSA header: the OSPFv2 Options@2 + 8-bit Type@3 become a single 16-bit LS Type@2.

Variables

View Source
var (
	// ErrShortBuffer reports a buffer shorter than a required fixed field.
	ErrShortBuffer = errors.New("ospfv3 packet: buffer too short")
	// ErrBadVersion reports a Version field that is not 3.
	ErrBadVersion = errors.New("ospfv3 packet: unsupported version")
	// ErrUnknownType reports a packet type outside 1..5.
	ErrUnknownType = errors.New("ospfv3 packet: unknown packet type")
	// ErrLength reports an internally inconsistent length (record misalignment,
	// declared length below the header minimum, or a count past the body).
	ErrLength = errors.New("ospfv3 packet: invalid length")
	// ErrTruncated reports a declared length that runs past the supplied buffer.
	ErrTruncated = errors.New("ospfv3 packet: truncated packet")
	// ErrUnknownLSAType reports an LS Type a typed body decoder does not handle.
	ErrUnknownLSAType = errors.New("ospfv3 packet: unknown LSA type")
)

Sentinel decode errors. They mirror the OSPFv2 codec's taxonomy so callers can branch on shape without coupling to OSPFv2.

Functions

func AppendSubTLVs

func AppendSubTLVs(fixed []byte, subs []ExtendedTLV) []byte

AppendSubTLVs returns fixed followed by the framed sub-TLVs -- the value of an Extended-LSA TLV that carries both fixed fields and nested sub-TLVs.

func EncodeExtendedLSABody

func EncodeExtendedLSABody(lsa ExtendedLSA) []byte

EncodeExtendedLSABody serializes the Extended-LSA body (the bytes after the 20-byte LSA header) buffer-first: each TLV is 4-octet aligned with its unpadded Length.

func FinalizeLSAChecksum

func FinalizeLSAChecksum(lsa []byte) uint16

FinalizeLSAChecksum computes and backfills the OSPFv3 LSA Fletcher checksum over lsa[2:length] (LS Age excluded), where length is the LSA header's Length field -- which the caller MUST have written before calling this. Bounding the coverage by Length (rather than len(lsa)) keeps it identical to VerifyLSAChecksum even if the caller passes an over-long buffer. The LSA checksum field must already be zeroed. RFC 5340 §A.4.2.1 keeps the OSPFv2 (RFC 2328 §12.1.7) Fletcher checksum unchanged. The result is non-zero by construction.

func FinalizePacketChecksum

func FinalizePacketChecksum(src, dst netip.Addr, pkt []byte) uint16

FinalizePacketChecksum computes the OSPFv3 packet checksum for pkt given the IPv6 source and destination and writes it into the checksum field at offChecksum, returning the value. PacketChecksum already excludes the checksum field from its own sum (internetSumZeroAt), so the field's prior contents do not matter; Packet.WriteTo conventionally leaves it zero. The transport calls this on TX once it knows the egress link-local source and the destination (RFC 5340 §A.3.1: the checksum binds the IPv6 pseudo-header). When an RFC 7166 Authentication Trailer is appended the checksum is left zero instead (RFC 7166 §2.2), so this is called only when no trailer is used.

func NSSAPropagate

func NSSAPropagate(l ExternalLSA) bool

NSSAPropagate reports whether an NSSA-LSA's prefix carries the P-bit (types.OptPrefixP) that asks the NSSA ABR to translate it to a Type 5 LSA.

func PacketChecksum

func PacketChecksum(src, dst netip.Addr, pkt []byte) uint16

PacketChecksum computes the OSPFv3 packet checksum: the IPv6 upper-layer checksum (RFC 1071 one's-complement) over the IPv6 pseudo-header (src, dst, 32-bit upper-layer length = len(pkt), three zero octets, Next Header 89) followed by the OSPF packet with the checksum field zeroed. Transport supplies src and dst; the codec cannot derive them from the packet bytes alone (RFC 5340 §A.3.1).

func PeekInstanceID

func PeekInstanceID(pkt []byte) (types.InstanceID, bool)

PeekInstanceID reads the OSPFv3 Instance ID (common-header byte 14) without decoding the rest of the packet. It returns false if pkt is shorter than the 16-octet common header. The transport uses this for its per-interface Instance ID demux (RFC 5340 §4.2.1: discard a packet whose Instance ID does not match the receiving interface); it is the only header field the transport reads -- full header validation stays in DecodeHeader and the ospfv3-4 dispatcher.

func VerifyLSAChecksum

func VerifyLSAChecksum(lsa []byte) bool

VerifyLSAChecksum verifies the Fletcher checksum over lsa[2:length].

func VerifyPacketChecksum

func VerifyPacketChecksum(src, dst netip.Addr, pkt []byte) bool

VerifyPacketChecksum verifies the OSPFv3 packet checksum. A spoofed source or destination changes the pseudo-header and fails verification (RFC 5340 §A.3.1).

Types

type DBDesc

type DBDesc struct {
	Options      types.Options
	InterfaceMTU uint16
	Flags        uint8
	DDSequence   uint32
	Headers      []LSAHeader
}

DBDesc is the OSPFv3 Database Description packet body.

func DecodeDBDesc

func DecodeDBDesc(body []byte) (DBDesc, error)

DecodeDBDesc parses a Database Description body. The trailing LSA headers must be a whole number of 20-octet headers.

func (DBDesc) EncodedLen

func (d DBDesc) EncodedLen() int

EncodedLen returns the Database Description body length.

func (DBDesc) WriteTo

func (d DBDesc) WriteTo(buf []byte, off int) int

WriteTo serializes the Database Description body into buf at off. The leading reserved octet, the reserved octet before the flags, and the high reserved bits of the flags octet are written zero (RFC 5340 §A.3.3).

type ExtendedLSA

type ExtendedLSA struct {
	TLVs []ExtendedTLV
}

ExtendedLSA is a decoded RFC 8362 Extended-LSA body: its top-level TLVs in wire order.

func DecodeExtendedLSABody

func DecodeExtendedLSABody(body []byte) (ExtendedLSA, error)

DecodeExtendedLSABody parses an Extended-LSA body into its top-level TLVs. A malformed TLV (truncated header or an over-length value) makes the whole body malformed and returns an error; a missing pad on the FINAL TLV is tolerated (be liberal on receive, see tlvIterator); the parser never panics (RFC 8362 §4).

type ExtendedTLV

type ExtendedTLV struct {
	Type  uint16
	Value []byte
}

ExtendedTLV is one RFC 8362 Extended-LSA TLV: its type and its full value bytes (the type-specific fixed fields followed by any nested sub-TLVs). Value is a caller -owned copy on decode.

func SubTLVsAt

func SubTLVsAt(value []byte, fixedLen int) ([]ExtendedTLV, error)

SubTLVsAt parses the nested sub-TLVs of a TLV value starting at fixedLen (the length of the TLV type's fixed-field prefix). A fixedLen at or beyond the value returns no sub-TLVs (a TLV may carry only fixed fields).

type ExternalLSA

type ExternalLSA struct {
	ExternalType2     bool // E-bit
	Metric            uint32
	Prefix            Prefix
	ReferencedLSType  types.LSType
	ForwardingAddr    [16]byte // present iff F-bit
	HasForwardingAddr bool
	ExternalRouteTag  uint32 // present iff T-bit
	HasRouteTag       bool
	ReferencedLSID    [4]byte // present iff ReferencedLSType != 0
}

ExternalLSA is the OSPFv3 AS-External-LSA (Type 0x4005) or NSSA-LSA (Type 0x2007) body; the two share an identical layout (RFC 5340 §A.4.7-A.4.8) and differ only by LS Type and flooding scope (AS-External is AS scope with the U-bit set; NSSA is area scope). The optional trailing fields are gated, in RFC order, by the F-bit (Forwarding Address, 128 bits), the T-bit (External Route Tag, 32 bits), and a non-zero Referenced LS Type (Referenced Link State ID, 32 bits). The 16-bit Referenced LS Type is carried in ReferencedLSType, not in the embedded Prefix's Field16 (which stays zero for this body).

func DecodeExternalLSA

func DecodeExternalLSA(body []byte) (ExternalLSA, error)

DecodeExternalLSA parses an AS-External / NSSA body. The optional trailing fields are consumed in RFC order (Forwarding Address, External Route Tag, Referenced Link State ID) and the body must end exactly after the last present field.

func (ExternalLSA) EncodedLen

func (l ExternalLSA) EncodedLen() int

EncodedLen returns the AS-External / NSSA body length.

func (ExternalLSA) WriteTo

func (l ExternalLSA) WriteTo(buf []byte, off int) int

WriteTo serializes the AS-External / NSSA body into buf at off. Byte 0 carries the E/F/T flags (E for a Type 2 metric, F when a Forwarding Address is present, T when an External Route Tag is present); the 24-bit Metric, PrefixLength, PrefixOptions, the 16-bit Referenced LS Type, and the AddressPrefix follow, then the present optional fields in RFC order.

type GraceLSA

type GraceLSA struct {
	GracePeriod uint32 // seconds neighbors keep advertising the router as adjacent
	Reason      uint8  // restart reason code, 0..3
}

GraceLSA is the OSPFv3 Grace-LSA body (RFC 5187 §2.2). The LS Type (0x000B) and the Link State ID (the originating Interface ID) live in the LSA header; this struct is the tlv-encoded body only.

func (GraceLSA) EncodedLen

func (g GraceLSA) EncodedLen() int

EncodedLen returns the Grace-LSA body length (the two 4-byte-aligned TLVs = 16 octets).

func (GraceLSA) WriteTo

func (g GraceLSA) WriteTo(buf []byte, off int) int

WriteTo serializes the Grace-LSA body (the two mandatory TLVs) into buf at off and returns the new offset. Buffer-first: the caller owns buf and sizes it for EncodedLen.

type Header struct {
	Type       PacketType
	Length     uint16
	RouterID   types.RouterID
	AreaID     types.AreaID
	Checksum   uint16
	InstanceID types.InstanceID
}

Header is the parsed 16-octet OSPFv3 common header. There is no AuType or Authentication field; Instance ID and Reserved occupy the reclaimed octets.

func DecodeHeader

func DecodeHeader(buf []byte) (Header, int, error)

DecodeHeader parses and validates the 16-octet OSPFv3 common header. It validates Version == 3, packet type 1..5, and Packet Length against the caller slice before any body parser runs.

func (Header) WriteTo

func (h Header) WriteTo(buf []byte, off int) int

WriteTo writes the common header as-is into buf at off and returns off+16. Packet.WriteTo usually backfills Length and Checksum after writing the body. The Reserved octet is always written zero (RFC 5340 §A.3.1).

type Hello

type Hello struct {
	InterfaceID        types.InterfaceID
	Priority           uint8
	Options            types.Options
	HelloInterval      uint16
	RouterDeadInterval uint16
	DR                 types.RouterID
	BDR                types.RouterID
	Neighbors          []types.RouterID
}

Hello is the OSPFv3 Hello packet body. There is no network mask: OSPFv3 runs per link and identifies the interface with a 32-bit Interface ID.

func DecodeHello

func DecodeHello(body []byte) (Hello, error)

DecodeHello parses a Hello body. The neighbor count is derived from the remaining body length, which must be a whole number of 4-octet Router IDs.

func (Hello) EncodedLen

func (h Hello) EncodedLen() int

EncodedLen returns the Hello body length.

func (Hello) WriteTo

func (h Hello) WriteTo(buf []byte, off int) int

WriteTo serializes the Hello body into buf at off.

type InterAreaPrefixLSA

type InterAreaPrefixLSA struct {
	Metric uint32
	Prefix Prefix
}

InterAreaPrefixLSA is the OSPFv3 Inter-Area-Prefix-LSA body (RFC 5340 §A.4.5): an area-scoped summary of an IPv6 prefix reachable in another area, with a 24-bit metric and an inlined prefix.

func (InterAreaPrefixLSA) EncodedLen

func (l InterAreaPrefixLSA) EncodedLen() int

EncodedLen returns the Inter-Area-Prefix-LSA body length.

func (InterAreaPrefixLSA) WriteTo

func (l InterAreaPrefixLSA) WriteTo(buf []byte, off int) int

WriteTo serializes the Inter-Area-Prefix-LSA body into buf at off. The leading reserved octet and the 16-bit reserved field are written zero (RFC 5340 §A.4.5).

type InterAreaRouterLSA

type InterAreaRouterLSA struct {
	Options           types.Options
	Metric            uint32
	DestinationRouter types.RouterID
}

InterAreaRouterLSA is the OSPFv3 Inter-Area-Router-LSA body (RFC 5340 §A.4.6): an area-scoped summary of the cost to reach an AS boundary router in another area. It is a fixed 12 octets.

func (InterAreaRouterLSA) EncodedLen

func (l InterAreaRouterLSA) EncodedLen() int

EncodedLen returns the Inter-Area-Router-LSA body length (fixed 12 octets).

func (InterAreaRouterLSA) WriteTo

func (l InterAreaRouterLSA) WriteTo(buf []byte, off int) int

WriteTo serializes the Inter-Area-Router-LSA body into buf at off. The two reserved octets (before Options and before Metric) are written zero (RFC 5340 §A.4.6).

type IntraAreaPrefixLSA

type IntraAreaPrefixLSA struct {
	ReferencedLSType      types.LSType
	ReferencedLinkStateID types.LinkStateID
	ReferencedAdvRouter   types.RouterID
	Prefixes              []Prefix
}

IntraAreaPrefixLSA is the OSPFv3 Intra-Area-Prefix-LSA body (RFC 5340 §A.4.10). It attaches IPv6 prefixes to the Router-LSA or Network-LSA named by the Referenced (LS Type, Link State ID, Advertising Router) triple. Each prefix's 16-bit field carries the prefix's Metric.

func (IntraAreaPrefixLSA) EncodedLen

func (l IntraAreaPrefixLSA) EncodedLen() int

EncodedLen returns the Intra-Area-Prefix-LSA body length.

func (IntraAreaPrefixLSA) WriteTo

func (l IntraAreaPrefixLSA) WriteTo(buf []byte, off int) int

WriteTo serializes the Intra-Area-Prefix-LSA body into buf at off. Each prefix's 16-bit field is its Metric (RFC 5340 §A.4.10), preserved from the decoded Prefix.Field16.

type LSA

type LSA struct {
	Header   LSAHeader
	Body     []byte
	RawBytes []byte

	Router       *RouterLSA
	Network      *NetworkLSA
	InterAreaPfx *InterAreaPrefixLSA
	InterAreaRtr *InterAreaRouterLSA
	External     *ExternalLSA
	Link         *LinkLSA
	IntraAreaPfx *IntraAreaPrefixLSA
	Grace        *GraceLSA
}

LSA is a lazily decoded LSA or a struct ready for encoding. DecodeLSA fills RawBytes and Body as caller-owned slices. Body-specific Decode* methods parse Body on demand. WriteTo re-emits RawBytes verbatim when no typed body is set, enabling opaque passthrough of received and unknown LSAs; constructed LSAs recompute Length and the Fletcher checksum.

func DecodeLSA

func DecodeLSA(buf []byte) (LSA, error)

DecodeLSA parses one LSA from the front of buf, driven by the Length field. The typed body is NOT eagerly decoded; RawBytes and Body retain caller-owned spans so flooding can re-emit a received LSA byte-for-byte.

func (*LSA) DecodeExternal

func (l *LSA) DecodeExternal() (ExternalLSA, error)

DecodeExternal parses an AS-External / NSSA body on demand.

func (*LSA) DecodeGrace

func (l *LSA) DecodeGrace() (GraceLSA, error)

DecodeGrace parses a Grace-LSA body (RFC 5187 §2.2) on demand.

func (*LSA) DecodeInterAreaPrefix

func (l *LSA) DecodeInterAreaPrefix() (InterAreaPrefixLSA, error)

DecodeInterAreaPrefix parses an Inter-Area-Prefix-LSA body on demand.

func (*LSA) DecodeInterAreaRouter

func (l *LSA) DecodeInterAreaRouter() (InterAreaRouterLSA, error)

DecodeInterAreaRouter parses an Inter-Area-Router-LSA body on demand.

func (*LSA) DecodeIntraAreaPrefix

func (l *LSA) DecodeIntraAreaPrefix() (IntraAreaPrefixLSA, error)

DecodeIntraAreaPrefix parses an Intra-Area-Prefix-LSA body on demand.

func (l *LSA) DecodeLink() (LinkLSA, error)

DecodeLink parses a Link-LSA body on demand.

func (*LSA) DecodeNetwork

func (l *LSA) DecodeNetwork() (NetworkLSA, error)

DecodeNetwork parses a Network-LSA body on demand.

func (*LSA) DecodeRouter

func (l *LSA) DecodeRouter() (RouterLSA, error)

DecodeRouter parses a Router-LSA body on demand.

func (*LSA) EncodedLen

func (l *LSA) EncodedLen() int

EncodedLen returns the total on-wire length of the LSA.

func (*LSA) VerifyChecksum

func (l *LSA) VerifyChecksum() bool

VerifyChecksum verifies this decoded LSA's Fletcher checksum.

func (*LSA) WriteTo

func (l *LSA) WriteTo(buf []byte, off int) int

WriteTo serializes the LSA into buf at off and returns the new offset. A received LSA with no typed body is re-emitted verbatim from RawBytes so a flooded LSA is byte-for-byte identical (RFC 5340 §4.5: LSAs are flooded unchanged). A constructed LSA recomputes Length and the Fletcher checksum.

type LSAHeader

type LSAHeader struct {
	Age               types.LSAge
	Type              types.LSType
	LinkStateID       types.LinkStateID
	AdvertisingRouter types.RouterID
	Sequence          types.LSSequenceNumber
	Checksum          uint16
	Length            uint16
}

LSAHeader is the decoded common OSPFv3 LSA header. Length covers this 20-octet header plus the body (RFC 5340 §A.4.2.1).

func DecodeLSAHeader

func DecodeLSAHeader(buf []byte) (LSAHeader, error)

DecodeLSAHeader parses the first 20 bytes of buf as an OSPFv3 LSA header. The LS Type is kept even when unrecognized: RFC 5340 §A.4.2.1's U-bit governs whether an unknown LSA is flooded, so the codec retains every type and leaves the policy to the LSDB.

func (LSAHeader) Key

func (h LSAHeader) Key() types.LSAKey

Key returns the LSDB identity tuple for this LSA header (RFC 5340 §A.4.2.1: an LSA is identified by LS Type, Link State ID, Advertising Router).

func (LSAHeader) WriteTo

func (h LSAHeader) WriteTo(buf []byte, off int) int

WriteTo writes the LSA header as-is into buf at off and returns off+20.

type LSAIterator

type LSAIterator struct {
	// contains filtered or unexported fields
}

LSAIterator walks a region of consecutive LSAs using each LSA's Length field. It never panics on malformed input; Err reports truncation or a bad length.

func NewLSAIterator

func NewLSAIterator(data []byte) LSAIterator

NewLSAIterator returns an iterator over a consecutive LSA region.

func (*LSAIterator) Err

func (it *LSAIterator) Err() error

Err returns the first iterator error, if any.

func (*LSAIterator) LSA

func (it *LSAIterator) LSA() LSA

LSA returns the current iterator item.

func (*LSAIterator) Next

func (it *LSAIterator) Next() bool

Next advances to the next LSA, bound-checking the Length against the buffer.

type LSAck

type LSAck struct {
	Headers []LSAHeader
}

LSAck is the OSPFv3 Link State Acknowledgment packet body: a list of 20-octet LSA headers (RFC 5340 §A.3.6).

func DecodeLSAck

func DecodeLSAck(body []byte) (LSAck, error)

DecodeLSAck parses an LS Ack body containing consecutive LSA headers.

func (LSAck) EncodedLen

func (a LSAck) EncodedLen() int

EncodedLen returns the LS Ack body length.

func (LSAck) WriteTo

func (a LSAck) WriteTo(buf []byte, off int) int

WriteTo serializes the LS Ack body into buf at off.

type LSReq

type LSReq struct {
	Requests []LSRequestEntry
}

LSReq is the OSPFv3 Link State Request packet body.

func DecodeLSReq

func DecodeLSReq(body []byte) (LSReq, error)

DecodeLSReq parses a Link State Request body. Entries must align to 12 octets. Unknown LS Types are retained verbatim: a peer may request an LSA whose function code this router does not implement, and the request is still valid.

func (LSReq) EncodedLen

func (r LSReq) EncodedLen() int

EncodedLen returns the LS Request body length.

func (LSReq) WriteTo

func (r LSReq) WriteTo(buf []byte, off int) int

WriteTo serializes the LS Request body into buf at off. The two leading reserved octets of every entry are written zero (RFC 5340 §A.3.4).

type LSRequestEntry

type LSRequestEntry struct {
	Type              types.LSType
	LinkStateID       types.LinkStateID
	AdvertisingRouter types.RouterID
}

LSRequestEntry is one Link State Request triple. RFC 5340 identifies a requested LSA by (LS Type, Link State ID, Advertising Router); the request carries no age, sequence, or checksum.

type LSUpdate

type LSUpdate struct {
	LSAs []LSA
}

LSUpdate is the OSPFv3 Link State Update packet body. Each LSA uses the 20-octet OSPFv3 LSA header and is self-delimited by its Length field.

func DecodeLSUpdate

func DecodeLSUpdate(body []byte) (LSUpdate, error)

DecodeLSUpdate parses a Link State Update body. The 32-bit "# LSAs" count is validated against the maximum number of minimum-size LSAs the body could hold before any allocation, then iteration is driven by each LSA Length field and must consume the body exactly (RFC 5340 §A.3.5).

func (*LSUpdate) EncodedLen

func (u *LSUpdate) EncodedLen() int

EncodedLen returns the LS Update body length.

func (*LSUpdate) WriteTo

func (u *LSUpdate) WriteTo(buf []byte, off int) int

WriteTo serializes the LS Update body into buf at off.

type LinkLSA

type LinkLSA struct {
	RtrPriority   uint8
	Options       types.Options
	LinkLocalAddr [16]byte
	Prefixes      []Prefix
}

LinkLSA is the OSPFv3 Link-LSA body (RFC 5340 §A.4.9). It is link-local scope (never flooded off the originating link) and advertises the router's link-local address, its Options for the link, and the IPv6 prefixes the router associates with the link. Each prefix's 16-bit field is Reserved (0).

func (LinkLSA) EncodedLen

func (l LinkLSA) EncodedLen() int

EncodedLen returns the Link-LSA body length.

func (LinkLSA) WriteTo

func (l LinkLSA) WriteTo(buf []byte, off int) int

WriteTo serializes the Link-LSA body into buf at off. Each prefix's 16-bit field is forced to Reserved (0) on the wire (RFC 5340 §A.4.9).

type NetworkLSA

type NetworkLSA struct {
	Options         types.Options
	AttachedRouters []types.RouterID
}

NetworkLSA is the OSPFv3 Network-LSA body (RFC 5340 §A.4.4). Unlike OSPFv2 it carries NO network mask: the header's Link State ID is the Designated Router's Interface ID and is preserved verbatim by the common LSA header. The attached router count is derived from the LSA Length.

func DecodeNetworkLSA

func DecodeNetworkLSA(body []byte) (NetworkLSA, error)

DecodeNetworkLSA parses a Network-LSA body. The attached-router count is derived from the remaining length, which must be a whole number of Router IDs.

func (NetworkLSA) EncodedLen

func (l NetworkLSA) EncodedLen() int

EncodedLen returns the Network-LSA body length.

func (NetworkLSA) WriteTo

func (l NetworkLSA) WriteTo(buf []byte, off int) int

WriteTo serializes the Network-LSA body into buf at off. The leading reserved octet is written zero (RFC 5340 §A.4.4).

type Packet

type Packet struct {
	Header   Header
	Hello    *Hello
	DBDesc   *DBDesc
	LSReq    *LSReq
	LSUpdate *LSUpdate
	LSAck    *LSAck
	RawBytes []byte
}

Packet is a decoded or encodable OSPFv3 packet. Exactly one body pointer is non-nil for packets produced by DecodePacket or by the runtime before encode.

func DecodePacket

func DecodePacket(buf []byte) (Packet, error)

DecodePacket parses a complete OSPFv3 payload beginning at the common header.

func (Packet) EncodedLen

func (p Packet) EncodedLen() int

EncodedLen returns the total packet length in octets (header + body).

func (*Packet) WriteTo

func (p *Packet) WriteTo(buf []byte, off int) int

WriteTo serializes the packet into buf at off and returns the new offset. The Packet Length field is backfilled after the body is written. The Checksum field is left zero here: RFC 5340 §A.3.1 binds the packet checksum to the IPv6 pseudo-header (source and destination supplied by transport), so a caller that wants the on-wire checksum calls FinalizePacketChecksum with the addresses after WriteTo. When an RFC 7166 Authentication Trailer is appended the checksum computation is omitted (RFC 7166 §2.2), so leaving it zero is also correct there.

type PacketType

type PacketType uint8

PacketType is the 1-octet OSPFv3 packet type field (RFC 5340 §A.3.1).

const (
	PacketTypeHello    PacketType = 1
	PacketTypeDBDesc   PacketType = 2
	PacketTypeLSReq    PacketType = 3
	PacketTypeLSUpdate PacketType = 4
	PacketTypeLSAck    PacketType = 5
)

OSPFv3 packet types (RFC 5340 §A.3.1). Identical numbering to OSPFv2.

func (PacketType) String

func (t PacketType) String() string

String renders the packet type as a stable lowercase token for diagnostics.

type Prefix

type Prefix struct {
	Length  types.PrefixLength
	Options types.PrefixOptions
	Field16 uint16
	Address []byte
}

Prefix is a decoded OSPFv3 IPv6 prefix (RFC 5340 §A.4.1). Field16 is the type-specific 16-bit field that follows PrefixOptions: a Metric in the Intra-Area-Prefix-LSA, Reserved (0) in the Link-LSA, and Reserved (0) in the inlined inter-area/external carriers. Address holds exactly ByteLen octets of the prefix, padded to a 32-bit word with zero bits past the prefix length.

type RouterLSA

type RouterLSA struct {
	Flags   uint8
	Options types.Options
	Links   []RouterLink
}

RouterLSA is the OSPFv3 Router-LSA body (RFC 5340 §A.4.3). Unlike OSPFv2 it carries NO IP addresses and NO per-link count: the number of link records is derived from the LSA Length. Options is the 24-bit OSPFv3 Options field.

func DecodeRouterLSA

func DecodeRouterLSA(body []byte) (RouterLSA, error)

DecodeRouterLSA parses a Router-LSA body. The link count is derived from the remaining body length, which must be a whole number of 16-octet records.

func (RouterLSA) EncodedLen

func (l RouterLSA) EncodedLen() int

EncodedLen returns the Router-LSA body length.

func (RouterLSA) WriteTo

func (l RouterLSA) WriteTo(buf []byte, off int) int

WriteTo serializes the Router-LSA body into buf at off. The reserved octet after Type in each link record is written zero (RFC 5340 §A.4.3).

type RouterLink struct {
	Type                uint8
	Metric              uint16
	InterfaceID         types.InterfaceID
	NeighborInterfaceID types.InterfaceID
	NeighborRouterID    types.RouterID
}

RouterLink is one 16-octet OSPFv3 Router-LSA link record (RFC 5340 §A.4.3). All three IDs are 32-bit; there are no IP addresses.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL