Expand description
BGP-4 wire format: encoding, decoding, and the model-facing JSON projection.
§Why netgauze rather than a hand-rolled codec
Everything on this path is attacker-controlled. Capability TLVs, path-attribute flags,
extended attribute lengths, the two-byte/four-byte AS_PATH ambiguity of RFC 6793 and the
multiprotocol attributes of RFC 4760 are all places where a plausible-looking hand-rolled
parser silently disagrees with a real router. netgauze-bgp-pkt was already a declared
dependency of the bgp feature and was not used by a single line of code; it is a
maintained implementation of RFC 4271 and the surrounding RFCs, so all parsing and the
structurally interesting encodings (OPEN, UPDATE) now go through it.
Access is via netgauze_bgp_pkt::codec::BgpCodec, which implements tokio_util’s
Encoder/Decoder. That matters for a mundane reason: netgauze’s more direct
WritablePdu/ReadablePduWithOneInput traits live in netgauze-parse-utils, which is not
a declared dependency of this crate, and neither is ipnet. The codec route needs only
netgauze-bgp-pkt, tokio-util and bytes, all of which are already present.
Two consequences of that constraint are visible below and both would disappear if
netgauze-parse-utils and ipnet were added to Cargo.toml:
ipv4_unicastbuilds anIpv4Unicastthrough serde instead ofIpv4Net.- A per-message codec is constructed rather than reusing one.
§What is deliberately not netgauze
KEEPALIVE and NOTIFICATION are hand-encoded. KEEPALIVE is a bare header. NOTIFICATION is a
header plus two octets, and netgauze models it as a closed enum of the (code, subcode) pairs
it knows, which cannot represent an arbitrary subcode. Since send_bgp_notification is an
LLM-facing action taking numeric codes, hand-encoding is both simpler and strictly more
expressive. Neither message has any structure to get wrong.
Structs§
- Decode
Error - A message that could not be parsed, together with the NOTIFICATION it earns.
- Update
Intent - Everything
send_bgp_updateneeds, already validated.
Enums§
- Header
Error - Why a 19-octet header was rejected, and the NOTIFICATION it earns.
Constants§
- AS_
TRANS - RFC 6793: the reserved two-octet ASN a four-octet speaker puts in the OPEN
My Autonomous Systemfield when its real ASN does not fit. - BGP_
HEADER_ LEN - Marker (16) + length (2) + type (1).
- BGP_
MARKER - Every BGP message begins with 16 octets of ones (RFC 4271 section 4.1).
- BGP_
MAX_ MESSAGE_ LEN - RFC 4271 section 4: no message may exceed 4096 octets. RFC 8654 raises this for peers that negotiate the extended-message capability; NetGet does not advertise it, so the hard limit applies in both directions and is enforced on input before a single body byte is read.
- ERR_
CEASE - ERR_FSM
- ERR_
HEADER - ERR_
HOLD_ TIMER_ EXPIRED - ERR_
OPEN - ERR_
UPDATE - MSG_
KEEPALIVE - MSG_
NOTIFICATION - MSG_
OPEN - BGP message type octets (RFC 4271 section 4.1).
- MSG_
ROUTE_ REFRESH - MSG_
UPDATE - SUB_
BAD_ BGP_ IDENTIFIER - SUB_
BAD_ MESSAGE_ LENGTH - SUB_
BAD_ MESSAGE_ TYPE - SUB_
BAD_ PEER_ AS - SUB_
CEASE_ ADMIN_ SHUTDOWN - SUB_
CEASE_ CONNECTION_ REJECTED - RFC 4486 section 4: the speaker is rejecting this connection. Permanent as far as the peer is concerned — it should not treat the rejection as a transient resource condition.
- SUB_
CEASE_ OUT_ OF_ RESOURCES - RFC 4486 section 8: the speaker cannot carry the peering right now for want of resources. A conforming implementation applies a damping/backoff to this subcode and retries, which is exactly the behaviour a saturated backend wants.
- SUB_
CONNECTION_ NOT_ SYNCHRONIZED - SUB_
FSM_ ESTABLISHED - SUB_
FSM_ OPENCONFIRM - SUB_
FSM_ OPENSENT - SUB_
UNACCEPTABLE_ HOLD_ TIME - SUB_
UNSUPPORTED_ VERSION
Functions§
- build_
open - Build our OPEN.
- build_
update - Build an UPDATE.
- capabilities_
to_ json - Describe our own capabilities and the peer’s for the
bgp_openevent. - decode
- Decode one complete BGP message (header included).
- encode
- Encode a message netgauze can represent (OPEN, UPDATE, KEEPALIVE, ROUTE-REFRESH).
- encode_
intent - Turn the validated intent produced by a
send_bgp_*action into wire bytes. - encode_
keepalive - A bare 19-octet KEEPALIVE (RFC 4271 section 4.4).
- encode_
notification - A NOTIFICATION (RFC 4271 section 4.5): header, error code, error subcode, opaque data.
- error_
name - Human-readable RFC 4271 section 6 error names, for logs and for the model.
- error_
subcode_ name - ipv4_
unicast - Parse
a.b.c.d/leninto a netgauze NLRI prefix, masking host bits. - parse_
header - Validate a BGP message header without touching the body.
- update_
to_ json - Render a decoded UPDATE as the structured JSON handed to the model.
Type Aliases§
- Notify
Code - A NOTIFICATION (error code, error subcode) pair.