rustbgpd-wire 0.11.0

BGP message codec โ€” encode/decode OPEN, KEEPALIVE, UPDATE, NOTIFICATION, ROUTE-REFRESH
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use bytes::{Buf, BufMut};

use crate::capability::{Afi, Safi};
use crate::constants::{HEADER_LEN, MARKER, message_type};
use crate::error::{DecodeError, EncodeError};
use crate::orf::{
    OrfPayload, decode_route_refresh_orf, encode_route_refresh_orf, route_refresh_orf_len,
};

/// ROUTE-REFRESH base body length (AFI u16 + subtype u8 + SAFI u8). An ORF
/// message (RFC 5291 ยง5.2) extends the body beyond this with ORF entries.
const BODY_LEN: usize = 4;

/// Total wire length of a plain ROUTE-REFRESH message (header + base body).
const TOTAL_LEN: usize = HEADER_LEN + BODY_LEN;

/// ROUTE-REFRESH demarcation subtype (RFC 7313).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RouteRefreshSubtype {
    /// Normal route refresh request (subtype 0).
    Normal,
    /// Beginning of Route Refresh (subtype 1, RFC 7313).
    BoRR,
    /// End of Route Refresh (subtype 2, RFC 7313).
    EoRR,
    /// Unrecognized subtype value.
    Unknown(
        /// The raw subtype byte.
        u8,
    ),
}

impl RouteRefreshSubtype {
    /// Create from a raw subtype byte.
    #[must_use]
    pub fn from_u8(value: u8) -> Self {
        match value {
            0 => Self::Normal,
            1 => Self::BoRR,
            2 => Self::EoRR,
            other => Self::Unknown(other),
        }
    }

    /// Return the raw byte value for this subtype.
    #[must_use]
    pub fn as_u8(self) -> u8 {
        match self {
            Self::Normal => 0,
            Self::BoRR => 1,
            Self::EoRR => 2,
            Self::Unknown(value) => value,
        }
    }
}

/// BGP ROUTE-REFRESH message (RFC 2918 + RFC 7313 + RFC 5291 ORF).
///
/// Requests a peer to re-advertise its Adj-RIB-Out for the specified
/// address family. RFC 7313 reuses the third octet as a demarcation subtype
/// (BoRR/EoRR). Raw wire values are stored so that unknown AFI/SAFI or
/// subtype values can be decoded without error โ€” the transport layer decides
/// whether to act on or ignore them.
///
/// When the message body extends beyond the 4-byte AFI/Reserved/SAFI header,
/// the trailing bytes are an RFC 5291 ORF section, decoded into [`orf`]. A
/// plain or Enhanced Route Refresh has [`orf`] set to `None`.
///
/// [`orf`]: RouteRefreshMessage::orf
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RouteRefreshMessage {
    /// Raw AFI value from the wire.
    pub afi_raw: u16,
    /// Raw demarcation subtype byte from the wire.
    pub subtype_raw: u8,
    /// Raw SAFI value from the wire.
    pub safi_raw: u8,
    /// RFC 5291 ORF section, present when the body extends past the 4-byte
    /// header. `None` for a plain (RFC 2918) or Enhanced (RFC 7313) refresh.
    pub orf: Option<OrfPayload>,
}

impl RouteRefreshMessage {
    /// Create a normal (subtype 0) ROUTE-REFRESH from typed AFI/SAFI values.
    #[must_use]
    pub fn new(afi: Afi, safi: Safi) -> Self {
        Self::new_with_subtype(afi, safi, RouteRefreshSubtype::Normal)
    }

    /// Create a ROUTE-REFRESH with an explicit subtype.
    #[must_use]
    pub fn new_with_subtype(afi: Afi, safi: Safi, subtype: RouteRefreshSubtype) -> Self {
        Self {
            afi_raw: afi as u16,
            subtype_raw: subtype.as_u8(),
            safi_raw: safi as u8,
            orf: None,
        }
    }

    /// Create an ORF-carrying ROUTE-REFRESH (RFC 5291 ยง5.2). The third octet
    /// is Reserved (subtype 0) for an ORF message.
    #[must_use]
    pub fn new_with_orf(afi: Afi, safi: Safi, orf: OrfPayload) -> Self {
        Self {
            afi_raw: afi as u16,
            subtype_raw: 0,
            safi_raw: safi as u8,
            orf: Some(orf),
        }
    }

    /// Try to interpret the raw AFI as a known address family.
    #[must_use]
    pub fn afi(&self) -> Option<Afi> {
        Afi::from_u16(self.afi_raw)
    }

    /// Try to interpret the raw SAFI as a known sub-address family.
    #[must_use]
    pub fn safi(&self) -> Option<Safi> {
        Safi::from_u8(self.safi_raw)
    }

    /// Decode the demarcation subtype.
    #[must_use]
    pub fn subtype(&self) -> RouteRefreshSubtype {
        RouteRefreshSubtype::from_u8(self.subtype_raw)
    }

    /// Decode a ROUTE-REFRESH message body from a buffer.
    ///
    /// A 4-byte body is a plain/Enhanced refresh (`orf: None`). A longer body
    /// carries an RFC 5291 ORF section, decoded into `orf`. A malformed ORF
    /// *entry* does not fail the decode โ€” it is surfaced via
    /// [`crate::orf::OrfEntries::Malformed`] so the caller can apply the RFC
    /// 5291 ยง5.2 reset semantics instead of tearing the session down.
    ///
    /// # Errors
    ///
    /// Returns [`DecodeError`] if the body length is below 4 or the buffer is
    /// truncated relative to the declared body length. Unknown AFI/SAFI values
    /// and unknown subtypes are preserved.
    pub fn decode(buf: &mut impl Buf, body_len: usize) -> Result<Self, DecodeError> {
        if body_len < BODY_LEN {
            return Err(DecodeError::InvalidLength {
                length: u16::try_from(HEADER_LEN + body_len).unwrap_or(u16::MAX),
            });
        }
        if buf.remaining() < body_len {
            return Err(DecodeError::Incomplete {
                needed: body_len,
                available: buf.remaining(),
            });
        }

        let afi_raw = buf.get_u16();
        let subtype_raw = buf.get_u8();
        let safi_raw = buf.get_u8();

        let orf = if body_len > BODY_LEN {
            // Bound the ORF parse to the declared body so a buffer carrying
            // more than one message cannot over-read.
            let mut orf_buf = buf.copy_to_bytes(body_len - BODY_LEN);
            let family = Afi::from_u16(afi_raw).zip(Safi::from_u8(safi_raw));
            Some(decode_route_refresh_orf(&mut orf_buf, family)?)
        } else {
            None
        };

        Ok(Self {
            afi_raw,
            subtype_raw,
            safi_raw,
            orf,
        })
    }

    /// Encode a complete ROUTE-REFRESH message (header + body) into a buffer.
    ///
    /// # Errors
    ///
    /// Returns [`EncodeError`] if the total message length (with an ORF
    /// section) exceeds the 16-bit length field.
    pub fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError> {
        let total = self.encoded_len();
        let total_u16 = u16::try_from(total).map_err(|_| EncodeError::ValueOutOfRange {
            field: "route_refresh_message_length",
            value: total.to_string(),
        })?;
        buf.put_slice(&MARKER);
        buf.put_u16(total_u16);
        buf.put_u8(message_type::ROUTE_REFRESH);
        buf.put_u16(self.afi_raw);
        buf.put_u8(self.subtype_raw);
        buf.put_u8(self.safi_raw);
        if let Some(orf) = &self.orf {
            encode_route_refresh_orf(orf, buf)?;
        }
        Ok(())
    }

    /// Total encoded size on the wire (header + base body + any ORF section).
    #[must_use]
    pub fn encoded_len(&self) -> usize {
        TOTAL_LEN + self.orf.as_ref().map_or(0, route_refresh_orf_len)
    }
}

impl std::fmt::Display for RouteRefreshMessage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let subtype = match self.subtype() {
            RouteRefreshSubtype::Normal => "Normal".to_string(),
            RouteRefreshSubtype::BoRR => "BoRR".to_string(),
            RouteRefreshSubtype::EoRR => "EoRR".to_string(),
            RouteRefreshSubtype::Unknown(value) => format!("Unknown({value})"),
        };

        let orf = if let Some(payload) = &self.orf {
            format!(
                " ORF(when={:?}, groups={})",
                payload.when_to_refresh,
                payload.groups.len()
            )
        } else {
            String::new()
        };
        match (self.afi(), self.safi()) {
            (Some(afi), Some(safi)) => {
                write!(
                    f,
                    "ROUTE-REFRESH subtype={subtype} AFI={afi:?} SAFI={safi:?}{orf}"
                )
            }
            _ => write!(
                f,
                "ROUTE-REFRESH subtype={subtype} AFI={} SAFI={}{orf}",
                self.afi_raw, self.safi_raw
            ),
        }
    }
}

#[cfg(test)]
mod tests {
    use bytes::{Bytes, BytesMut};

    use super::*;

    #[test]
    fn roundtrip_ipv4_unicast() {
        let msg = RouteRefreshMessage::new(Afi::Ipv4, Safi::Unicast);
        let mut buf = BytesMut::with_capacity(TOTAL_LEN);
        msg.encode(&mut buf).unwrap();
        assert_eq!(buf.len(), TOTAL_LEN);

        let mut bytes = buf.freeze();
        bytes.advance(HEADER_LEN);
        let decoded = RouteRefreshMessage::decode(&mut bytes, BODY_LEN).unwrap();
        assert_eq!(decoded, msg);
        assert_eq!(decoded.afi(), Some(Afi::Ipv4));
        assert_eq!(decoded.safi(), Some(Safi::Unicast));
        assert_eq!(decoded.subtype(), RouteRefreshSubtype::Normal);
    }

    #[test]
    fn roundtrip_ipv6_unicast() {
        let msg = RouteRefreshMessage::new(Afi::Ipv6, Safi::Unicast);
        let mut buf = BytesMut::with_capacity(TOTAL_LEN);
        msg.encode(&mut buf).unwrap();

        let mut bytes = buf.freeze();
        bytes.advance(HEADER_LEN);
        let decoded = RouteRefreshMessage::decode(&mut bytes, BODY_LEN).unwrap();
        assert_eq!(decoded, msg);
        assert_eq!(decoded.afi(), Some(Afi::Ipv6));
        assert_eq!(decoded.safi(), Some(Safi::Unicast));
        assert_eq!(decoded.subtype(), RouteRefreshSubtype::Normal);
    }

    #[test]
    fn roundtrip_borr() {
        let msg = RouteRefreshMessage::new_with_subtype(
            Afi::Ipv4,
            Safi::Unicast,
            RouteRefreshSubtype::BoRR,
        );
        let mut buf = BytesMut::with_capacity(TOTAL_LEN);
        msg.encode(&mut buf).unwrap();
        let mut bytes = buf.freeze();
        bytes.advance(HEADER_LEN);
        let decoded = RouteRefreshMessage::decode(&mut bytes, BODY_LEN).unwrap();
        assert_eq!(decoded, msg);
        assert_eq!(decoded.subtype(), RouteRefreshSubtype::BoRR);
    }

    #[test]
    fn roundtrip_eorr() {
        let msg = RouteRefreshMessage::new_with_subtype(
            Afi::Ipv6,
            Safi::Unicast,
            RouteRefreshSubtype::EoRR,
        );
        let mut buf = BytesMut::with_capacity(TOTAL_LEN);
        msg.encode(&mut buf).unwrap();
        let mut bytes = buf.freeze();
        bytes.advance(HEADER_LEN);
        let decoded = RouteRefreshMessage::decode(&mut bytes, BODY_LEN).unwrap();
        assert_eq!(decoded, msg);
        assert_eq!(decoded.subtype(), RouteRefreshSubtype::EoRR);
    }

    #[test]
    fn plain_four_byte_body_has_no_orf() {
        // Regression: a 4-byte body is a plain/Enhanced refresh, never ORF.
        let data: &[u8] = &[0, 1, 0, 1];
        let mut buf = Bytes::copy_from_slice(data);
        let msg = RouteRefreshMessage::decode(&mut buf, 4).unwrap();
        assert_eq!(msg.orf, None);
    }

    #[test]
    fn reject_body_length_three() {
        let data: &[u8] = &[0, 1, 0];
        let mut buf = Bytes::copy_from_slice(data);
        assert!(RouteRefreshMessage::decode(&mut buf, 3).is_err());
    }

    #[test]
    fn reject_truncated_orf_body() {
        // body_len claims 10 but only 4 bytes are present.
        let data: &[u8] = &[0, 1, 0, 1];
        let mut buf = Bytes::copy_from_slice(data);
        assert!(RouteRefreshMessage::decode(&mut buf, 10).is_err());
    }

    #[test]
    fn roundtrip_orf_route_refresh() {
        use crate::nlri::{Ipv4Prefix, Prefix};
        use crate::orf::{
            AddressPrefixOrf, OrfAction, OrfEntries, OrfEntryGroup, OrfMatch, OrfPayload, OrfType,
            WhenToRefresh,
        };
        use std::net::Ipv4Addr;

        let payload = OrfPayload {
            when_to_refresh: WhenToRefresh::Immediate,
            groups: vec![OrfEntryGroup {
                orf_type: OrfType::AddressPrefix,
                entries: OrfEntries::AddressPrefix(vec![AddressPrefixOrf {
                    action: OrfAction::Add,
                    match_: OrfMatch::Permit,
                    sequence: 10,
                    min_len: 24,
                    max_len: 32,
                    prefix: Some(Prefix::V4(Ipv4Prefix::new(Ipv4Addr::new(10, 0, 0, 0), 8))),
                }]),
            }],
        };
        let msg = RouteRefreshMessage::new_with_orf(Afi::Ipv4, Safi::Unicast, payload);

        let mut buf = BytesMut::with_capacity(msg.encoded_len());
        msg.encode(&mut buf).unwrap();
        assert_eq!(buf.len(), msg.encoded_len());

        let mut bytes = buf.freeze();
        bytes.advance(HEADER_LEN);
        let body_len = msg.encoded_len() - HEADER_LEN;
        let decoded = RouteRefreshMessage::decode(&mut bytes, body_len).unwrap();
        assert_eq!(decoded, msg);
        assert!(decoded.orf.is_some());
    }

    #[test]
    fn decode_unknown_afi_succeeds() {
        let data: &[u8] = &[0, 99, 0, 1];
        let mut buf = Bytes::copy_from_slice(data);
        let msg = RouteRefreshMessage::decode(&mut buf, 4).unwrap();
        assert_eq!(msg.afi_raw, 99);
        assert_eq!(msg.afi(), None);
        assert_eq!(msg.safi(), Some(Safi::Unicast));
        assert_eq!(msg.subtype(), RouteRefreshSubtype::Normal);
    }

    #[test]
    fn decode_unknown_safi_succeeds() {
        let data: &[u8] = &[0, 1, 0, 99];
        let mut buf = Bytes::copy_from_slice(data);
        let msg = RouteRefreshMessage::decode(&mut buf, 4).unwrap();
        assert_eq!(msg.safi_raw, 99);
        assert_eq!(msg.safi(), None);
        assert_eq!(msg.afi(), Some(Afi::Ipv4));
        assert_eq!(msg.subtype(), RouteRefreshSubtype::Normal);
    }

    #[test]
    fn decode_orf_unknown_safi_preserves_address_prefix_group_as_raw() {
        use crate::constants::orf;
        use crate::orf::{OrfEntries, OrfType, WhenToRefresh};

        let data: &[u8] = &[
            0,
            1,
            0,
            128, // AFI IPv4, Reserved, future/unknown SAFI.
            orf::WHEN_IMMEDIATE,
            orf::TYPE_ADDRESS_PREFIX,
            0,
            8,
            orf::ACTION_ADD,
            0,
            0,
            0,
            1,
            0,
            0,
            40,
        ];
        let mut buf = Bytes::copy_from_slice(data);
        let msg = RouteRefreshMessage::decode(&mut buf, data.len()).unwrap();
        let payload = msg.orf.unwrap();
        assert_eq!(payload.when_to_refresh, WhenToRefresh::Immediate);
        assert_eq!(payload.groups[0].orf_type, OrfType::AddressPrefix);
        assert_eq!(
            payload.groups[0].entries,
            OrfEntries::Raw(Bytes::from_static(&[orf::ACTION_ADD, 0, 0, 0, 1, 0, 0, 40]))
        );
    }

    #[test]
    fn decode_unknown_subtype_succeeds() {
        let data: &[u8] = &[0, 1, 9, 1];
        let mut buf = Bytes::copy_from_slice(data);
        let msg = RouteRefreshMessage::decode(&mut buf, 4).unwrap();
        assert_eq!(msg.subtype(), RouteRefreshSubtype::Unknown(9));
    }

    #[test]
    fn encoded_len_is_23() {
        let msg = RouteRefreshMessage::new(Afi::Ipv4, Safi::Unicast);
        assert_eq!(msg.encoded_len(), 23);
    }

    #[test]
    fn display_known_family() {
        let msg = RouteRefreshMessage::new(Afi::Ipv6, Safi::Unicast);
        let s = format!("{msg}");
        assert!(s.contains("Ipv6"));
        assert!(s.contains("Unicast"));
        assert!(s.contains("Normal"));
    }

    #[test]
    fn display_unknown_family() {
        let msg = RouteRefreshMessage {
            afi_raw: 99,
            subtype_raw: 7,
            safi_raw: 42,
            orf: None,
        };
        let s = format!("{msg}");
        assert!(s.contains("99"));
        assert!(s.contains("42"));
        assert!(s.contains("Unknown(7)"));
    }
}