Skip to main content

rustbgpd_wire/
constants.rs

1/// The 16-byte marker field (all 0xFF) that begins every BGP message.
2pub const MARKER: [u8; 16] = [0xFF; 16];
3
4/// Length of the BGP message header in bytes.
5pub const HEADER_LEN: usize = 19;
6
7/// Minimum valid BGP message length (header only, used by KEEPALIVE).
8pub const MIN_MESSAGE_LEN: u16 = 19;
9
10/// Maximum valid BGP message length per RFC 4271 §4.1.
11pub const MAX_MESSAGE_LEN: u16 = 4096;
12
13/// Maximum BGP message length with Extended Messages (RFC 8654).
14pub const EXTENDED_MAX_MESSAGE_LEN: u16 = 65535;
15
16/// Size of the marker field in the header.
17pub const MARKER_LEN: usize = 16;
18
19/// BGP protocol version.
20pub const BGP_VERSION: u8 = 4;
21
22/// `AS_TRANS` value for 4-byte ASN backward compatibility (RFC 6793).
23pub const AS_TRANS: u16 = 23456;
24
25/// Default BGP TCP port.
26pub const BGP_PORT: u16 = 179;
27
28/// Minimum non-zero hold time in seconds (RFC 4271 §4.2).
29pub const MIN_HOLD_TIME: u16 = 3;
30
31/// Minimum OPEN message length (header + version + AS + hold + ID + opt len).
32pub const MIN_OPEN_LEN: u16 = 29;
33
34/// Minimum UPDATE message length (header + withdrawn len + attrs len).
35pub const MIN_UPDATE_LEN: u16 = 23;
36
37/// Minimum NOTIFICATION message length (header + code + subcode).
38pub const MIN_NOTIFICATION_LEN: u16 = 21;
39
40/// Message type codes.
41pub mod message_type {
42    /// OPEN message (type 1).
43    pub const OPEN: u8 = 1;
44    /// UPDATE message (type 2).
45    pub const UPDATE: u8 = 2;
46    /// NOTIFICATION message (type 3).
47    pub const NOTIFICATION: u8 = 3;
48    /// KEEPALIVE message (type 4).
49    pub const KEEPALIVE: u8 = 4;
50    /// ROUTE-REFRESH message (type 5, RFC 2918).
51    pub const ROUTE_REFRESH: u8 = 5;
52}
53
54/// OPEN optional parameter type codes (RFC 5492).
55pub mod param_type {
56    /// Capabilities Optional Parameter (RFC 5492).
57    pub const CAPABILITIES: u8 = 2;
58}
59
60/// Capability codes (IANA BGP Capability Codes registry).
61pub mod capability_code {
62    /// RFC 4760: Multi-Protocol Extensions.
63    pub const MULTI_PROTOCOL: u8 = 1;
64    /// RFC 2918: Route Refresh.
65    pub const ROUTE_REFRESH: u8 = 2;
66    /// RFC 8950: Extended Next Hop Encoding.
67    pub const EXTENDED_NEXT_HOP: u8 = 5;
68    /// RFC 4724: Graceful Restart.
69    pub const GRACEFUL_RESTART: u8 = 64;
70    /// RFC 8654: Extended Messages.
71    pub const EXTENDED_MESSAGE: u8 = 6;
72    /// RFC 7911: Add-Path.
73    pub const ADD_PATH: u8 = 69;
74    /// RFC 7313: Enhanced Route Refresh.
75    pub const ENHANCED_ROUTE_REFRESH: u8 = 70;
76    /// RFC 9494: Long-Lived Graceful Restart.
77    pub const LONG_LIVED_GRACEFUL_RESTART: u8 = 71;
78    /// RFC 6793: 4-Byte AS Number.
79    pub const FOUR_OCTET_AS: u8 = 65;
80    /// RFC 9234: BGP Role.
81    pub const BGP_ROLE: u8 = 9;
82    /// RFC 5291: Outbound Route Filtering (ORF).
83    pub const OUTBOUND_ROUTE_FILTERING: u8 = 3;
84}
85
86/// Outbound Route Filtering (ORF) wire constants (RFC 5291 + RFC 5292).
87pub mod orf {
88    /// Address-Prefix ORF-Type (RFC 5292, IANA-assigned). The standard value
89    /// advertised and applied by rustbgpd.
90    pub const TYPE_ADDRESS_PREFIX: u8 = 64;
91    /// Legacy (pre-standard Cisco) Address-Prefix ORF-Type. Decoded for
92    /// interoperability; never advertised. Applied only if negotiated.
93    pub const TYPE_ADDRESS_PREFIX_LEGACY: u8 = 128;
94
95    /// Capability Send/Receive (RFC 5291 §4): willing to receive ORF entries.
96    pub const SEND_RECEIVE_RECEIVE: u8 = 1;
97    /// Capability Send/Receive: willing to send ORF entries.
98    pub const SEND_RECEIVE_SEND: u8 = 2;
99    /// Capability Send/Receive: willing to both send and receive.
100    pub const SEND_RECEIVE_BOTH: u8 = 3;
101
102    /// ROUTE-REFRESH When-to-refresh (RFC 5291 §5.2): refresh immediately.
103    pub const WHEN_IMMEDIATE: u8 = 1;
104    /// ROUTE-REFRESH When-to-refresh: defer the re-advertisement sweep.
105    pub const WHEN_DEFER: u8 = 2;
106
107    /// Common ORF entry header — Action field mask (top 2 bits, RFC 5291 §5.1.1).
108    pub const ACTION_MASK: u8 = 0xC0;
109    /// Common ORF entry Action: ADD.
110    pub const ACTION_ADD: u8 = 0x00;
111    /// Common ORF entry Action: REMOVE.
112    pub const ACTION_REMOVE: u8 = 0x80;
113    /// Common ORF entry Action: REMOVE-ALL (entry carries only the header byte).
114    pub const ACTION_REMOVE_ALL: u8 = 0xC0;
115    /// Common ORF entry header — Match field mask (bit 5, RFC 5291 §5.1.1).
116    pub const MATCH_MASK: u8 = 0x20;
117    /// Common ORF entry Match: DENY (PERMIT is the mask cleared).
118    pub const MATCH_DENY: u8 = 0x20;
119}
120
121/// Path attribute type codes (RFC 4271 §5).
122pub mod attr_type {
123    /// `ORIGIN` (type 1).
124    pub const ORIGIN: u8 = 1;
125    /// `AS_PATH` (type 2).
126    pub const AS_PATH: u8 = 2;
127    /// `NEXT_HOP` (type 3).
128    pub const NEXT_HOP: u8 = 3;
129    /// `MULTI_EXIT_DISC` (type 4).
130    pub const MULTI_EXIT_DISC: u8 = 4;
131    /// `LOCAL_PREF` (type 5).
132    pub const LOCAL_PREF: u8 = 5;
133    /// `ATOMIC_AGGREGATE` (type 6).
134    pub const ATOMIC_AGGREGATE: u8 = 6;
135    /// `AGGREGATOR` (type 7).
136    pub const AGGREGATOR: u8 = 7;
137    /// `COMMUNITIES` (type 8, RFC 1997).
138    pub const COMMUNITIES: u8 = 8;
139    /// RFC 4456: `ORIGINATOR_ID`.
140    pub const ORIGINATOR_ID: u8 = 9;
141    /// RFC 4456: `CLUSTER_LIST`.
142    pub const CLUSTER_LIST: u8 = 10;
143    /// RFC 4360: Extended Communities.
144    pub const EXTENDED_COMMUNITIES: u8 = 16;
145    /// RFC 8092: Large Communities.
146    pub const LARGE_COMMUNITIES: u8 = 32;
147    /// RFC 4760: `MP_REACH_NLRI`.
148    pub const MP_REACH_NLRI: u8 = 14;
149    /// RFC 4760: `MP_UNREACH_NLRI`.
150    pub const MP_UNREACH_NLRI: u8 = 15;
151    /// RFC 6514 §5: PMSI Tunnel attribute (used by EVPN Type 3 IMET
152    /// for ingress-replication BUM).
153    pub const PMSI_TUNNEL: u8 = 22;
154    /// RFC 9234 §5: Only-to-Customer (OTC).
155    pub const ONLY_TO_CUSTOMER: u8 = 35;
156}
157
158/// Path attribute flag bits (RFC 4271 §4.3).
159pub mod attr_flags {
160    /// Bit 7: attribute is optional (vs. well-known).
161    pub const OPTIONAL: u8 = 0x80;
162    /// Bit 6: attribute is transitive.
163    pub const TRANSITIVE: u8 = 0x40;
164    /// Bit 5: attribute is partial (incomplete transitive).
165    pub const PARTIAL: u8 = 0x20;
166    /// Bit 4: attribute length is 2 bytes (vs. 1 byte).
167    pub const EXTENDED_LENGTH: u8 = 0x10;
168}
169
170/// `AS_PATH` segment types (RFC 4271 §4.3).
171pub mod as_path_segment {
172    /// `AS_SET` segment type.
173    pub const AS_SET: u8 = 1;
174    /// `AS_SEQUENCE` segment type.
175    pub const AS_SEQUENCE: u8 = 2;
176}