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
467
468
469
470
471
472
473
474
475
476
use byteorder::{BigEndian, ReadBytesExt};
use std::io::{Error, ErrorKind, Read};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use crate::Header;
use crate::AFI;

///
/// The BGP4MP enum represents all possible subtypes of the BGP4MP record type.
///
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub enum BGP4MP {
    /// Represents a state change of the BGP collector using 16 bit ASN.
    STATE_CHANGE(STATE_CHANGE),

    /// Represents UPDATE, OPEN, NOTIFICATION and KEEPALIVE messages supporting 16 bit ASN.
    MESSAGE(MESSAGE),

    /// Used to encode BGP RIB entries in older MRT files but is now superseded by TABLE_DUMP_V2.
    ENTRY(ENTRY),

    /// Represents a state change of the BGP collector using 32 bit ASN.
    SNAPSHOT(SNAPSHOT),

    /// Represents UPDATE, OPEN, NOTIFICATION and KEEPALIVE messages supporting 32 bit ASN.
    MESSAGE_AS4(MESSAGE_AS4),

    /// Represents a state change of the BGP collector using 32 bit ASN.
    STATE_CHANGE_AS4(STATE_CHANGE_AS4),

    /// A locally generated UPDATE, OPEN, NOTIFICATION and KEEPALIVE messages supporting 16 bit ASN.
    MESSAGE_LOCAL(MESSAGE),

    /// A locally generated UPDATE, OPEN, NOTIFICATION and KEEPALIVE messages supporting 32 bit ASN.
    MESSAGE_AS4_LOCAL(MESSAGE_AS4),

    /// Added by (RFC8050)[https://tools.ietf.org/html/rfc8050] to enable advertisement of multiple paths as defined in (RFC7911)[https://tools.ietf.org/html/rfc7911].
    /// This type signifies that 16-bit ASN are used and that the message is not locally generated.
    MESSAGE_ADDPATH(MESSAGE),

    /// Added by (RFC8050)[https://tools.ietf.org/html/rfc8050] to enable advertisement of multiple paths as defined in (RFC7911)[https://tools.ietf.org/html/rfc7911].
    /// This type signifies that 32bit ASN are used and that the message is not locally generated.
    MESSAGE_AS4_ADDPATH(MESSAGE_AS4),

    /// Added by (RFC8050)[https://tools.ietf.org/html/rfc8050] to enable advertisement of multiple paths as defined in (RFC7911)[https://tools.ietf.org/html/rfc7911].
    /// This type signifies that 16-bit ASN are used and that the message is locally generated.
    MESSAGE_LOCAL_ADDPATH(MESSAGE),

    /// Added by (RFC8050)[https://tools.ietf.org/html/rfc8050] to enable advertisement of multiple paths as defined in (RFC7911)[https://tools.ietf.org/html/rfc7911].
    /// This type signifies that 32-bit ASN are used and that the message is locally generated.
    MESSAGE_AS4_LOCAL_ADDPATH(MESSAGE_AS4),
}

///
/// Represents a state change in the BGP Finite State Machine (FSM).
/// More information can found in [RFC4271](https://tools.ietf.org/html/rfc4271#section-8).
///
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct STATE_CHANGE {
    /// The peer ASN from which the BGP message has been received.
    pub peer_as: u16,

    /// The ASN of the AS that received this BGP message.
    pub local_as: u16,

    /// The interface identifier to which this message applies.
    pub interface: u16,

    /// The peer IP address address from which the BGP message has been received.
    pub peer_address: IpAddr,

    /// The IP address of the AS that received this BGP message.
    pub local_address: IpAddr,

    /// The old state of the BGP collector.
    pub old_state: u16,

    /// The new state of the BGP collector.
    pub new_state: u16,
}

impl STATE_CHANGE {
    fn parse(stream: &mut Read) -> Result<STATE_CHANGE, Error> {
        let peer_as = stream.read_u16::<BigEndian>()?;
        let local_as = stream.read_u16::<BigEndian>()?;
        let interface = stream.read_u16::<BigEndian>()?;
        let afi = stream.read_u16::<BigEndian>()?;
        let peer_address = match AFI::from(afi)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };
        let local_address = match AFI::from(afi)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };
        let old_state = stream.read_u16::<BigEndian>()?;
        let new_state = stream.read_u16::<BigEndian>()?;

        Ok(STATE_CHANGE {
            peer_as,
            local_as,
            interface,
            peer_address,
            local_address,
            old_state,
            new_state,
        })
    }
}

/// Represents a BGP message (UPDATE, OPEN, NOTIFICATION and KEEPALIVE) using 16bit ASN.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct MESSAGE {
    /// The peer ASN from which the BGP message has been received.
    pub peer_as: u16,

    /// The ASN of the AS that received this BGP message.
    pub local_as: u16,

    /// The interface identifier to which this message applies.
    pub interface: u16,

    /// The peer IP address address from which the BGP message has been received.
    pub peer_address: IpAddr,

    /// The IP address of the AS that received this BGP message.
    pub local_address: IpAddr,

    /// The message that has been received.
    pub message: Vec<u8>,
}

impl MESSAGE {
    fn parse(header: &Header, stream: &mut Read) -> Result<MESSAGE, Error> {
        let peer_as = stream.read_u16::<BigEndian>()?;
        let local_as = stream.read_u16::<BigEndian>()?;
        let interface = stream.read_u16::<BigEndian>()?;
        let afi = stream.read_u16::<BigEndian>()?;
        let peer_address = match AFI::from(afi)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };
        let local_address = match AFI::from(afi)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };

        let length = header.length - (8 + 2 * AFI::from(afi)?.size());
        let mut message = vec![0; length as usize];
        stream.read_exact(&mut message)?;

        Ok(MESSAGE {
            peer_as,
            local_as,
            interface,
            peer_address,
            local_address,
            message,
        })
    }
}

/// Represents a BGP message (UPDATE, OPEN, NOTIFICATION and KEEPALIVE) using 32bit ASN.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct MESSAGE_AS4 {
    /// The peer ASN from which the BGP message has been received.
    pub peer_as: u32,

    /// The ASN of the AS that received this BGP message.
    pub local_as: u32,

    /// The interface identifier to which this message applies.
    pub interface: u16,

    /// The peer IP address address from which the BGP message has been received.
    pub peer_address: IpAddr,

    /// The IP address of the AS that received this BGP message.
    pub local_address: IpAddr,

    /// The message that has been received.
    pub message: Vec<u8>,
}

impl MESSAGE_AS4 {
    fn parse(header: &Header, stream: &mut Read) -> Result<MESSAGE_AS4, Error> {
        let peer_as = stream.read_u32::<BigEndian>()?;
        let local_as = stream.read_u32::<BigEndian>()?;
        let interface = stream.read_u16::<BigEndian>()?;
        let afi = stream.read_u16::<BigEndian>()?;
        let peer_address = match AFI::from(afi)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };
        let local_address = match AFI::from(afi)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };

        let length = header.length - (12 + 2 * AFI::from(afi)?.size());
        let mut message = vec![0; length as usize];
        stream.read_exact(&mut message)?;

        Ok(MESSAGE_AS4 {
            peer_as,
            local_as,
            interface,
            peer_address,
            local_address,
            message,
        })
    }
}

///
/// Represents a state change in the BGP Finite State Machine (FSM).
///
/// 1 Idle
/// 2 Connect
/// 3 Active
/// 4 OpenSent
/// 5 OpenConfirm
/// 6 Established
/// More information can found in [RFC4271](https://tools.ietf.org/html/rfc4271#section-8).
///
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct STATE_CHANGE_AS4 {
    /// The peer ASN from which the BGP message has been received.
    pub peer_as: u32,

    /// The ASN of the AS that received this BGP message.
    pub local_as: u32,

    /// The interface identifier to which this message applies.
    pub interface: u16,

    /// The peer IP address address from which the BGP message has been received.
    pub peer_address: IpAddr,

    /// The IP address of the AS that received this BGP message.
    pub local_address: IpAddr,

    /// The old state of the BGP collector.
    pub old_state: u16,

    /// The new state of the BGP collector.
    pub new_state: u16,
}

impl STATE_CHANGE_AS4 {
    fn parse(stream: &mut Read) -> Result<STATE_CHANGE_AS4, Error> {
        let peer_as = stream.read_u32::<BigEndian>()?;
        let local_as = stream.read_u32::<BigEndian>()?;
        let interface = stream.read_u16::<BigEndian>()?;
        let afi = stream.read_u16::<BigEndian>()?;
        let peer_address = match AFI::from(afi)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };
        let local_address = match AFI::from(afi)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };
        let old_state = stream.read_u16::<BigEndian>()?;
        let new_state = stream.read_u16::<BigEndian>()?;

        Ok(STATE_CHANGE_AS4 {
            peer_as,
            local_as,
            interface,
            peer_address,
            local_address,
            old_state,
            new_state,
        })
    }
}

/// Deprecated: Used to record BGP4MP messages in a file.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct SNAPSHOT {
    /// The associated view number.
    pub view_number: u16,

    /// The NULL-terminated filename of the file where ENTRY records are recorded.
    pub filename: Vec<u8>,
}

impl SNAPSHOT {
    fn parse(stream: &mut Read) -> Result<SNAPSHOT, Error> {
        let view_number = stream.read_u16::<BigEndian>()?;
        let mut filename = Vec::new();

        let mut buffer = stream.read_u8()?;
        while buffer != b'\0' {
            filename.push(buffer);
            buffer = stream.read_u8()?;
        }

        Ok(SNAPSHOT {
            view_number,
            filename,
        })
    }
}

/// Used to record RIB table entries but has not seen wide support.
/// More information can found in [RFC6396](https://tools.ietf.org/html/rfc6396#appendix-B.2.6).
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct ENTRY {
    /// The peer ASN from which the BGP message has been received.
    pub peer_as: u16,

    /// The ASN of the AS that received this BGP message.
    pub local_as: u16,

    /// The interface identifier to which this message applies.
    pub interface: u16,

    /// The peer IP address address from which the BGP message has been received.
    pub peer_address: IpAddr,

    /// The IP address of the AS that received this BGP message.
    pub local_address: IpAddr,

    /// The associated view number.
    pub view_number: u16,

    /// Status bits.
    pub status: u16,

    /// The last time that this route has been changed.
    pub time_last_change: u32,

    /// Represents the address of the next hop of this route.
    pub next_hop: IpAddr,

    /// The Address Family Identifier (AFI) of the NLRI.
    pub afi: u16,

    /// The Subsequent Address Family Identifier (SAFI) of the NLRI.
    pub safi: u8,

    /// The prefix length of the prefix.
    pub prefix_length: u8,

    /// The prefix in bytes rounded up to the nearest byte.
    pub prefix: Vec<u8>,

    /// The BGP Path attributes associated with this route.
    pub attributes: Vec<u8>,
}

impl ENTRY {
    fn parse(stream: &mut Read) -> Result<ENTRY, Error> {
        let peer_as = stream.read_u16::<BigEndian>()?;
        let local_as = stream.read_u16::<BigEndian>()?;
        let interface = stream.read_u16::<BigEndian>()?;

        let afi = stream.read_u16::<BigEndian>()?;
        let peer_address = match AFI::from(afi)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };
        let local_address = match AFI::from(afi)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };

        let view_number = stream.read_u16::<BigEndian>()?;
        let status = stream.read_u16::<BigEndian>()?;
        let time_last_change = stream.read_u32::<BigEndian>()?;

        // Read the AFI and SAFI belonging to the prefix.
        let afi = stream.read_u16::<BigEndian>()?;
        let safi = stream.read_u8()?;

        // Read the next hop.
        let next_hop_length = stream.read_u8()?;
        let next_hop = match next_hop_length {
            4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            16 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
            x => {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("Unknown NEXT_HOP length in BGP4MP::ENTRY: {}", x),
                ));
            }
        };

        // Read the prefix.
        let prefix_length: u8 = stream.read_u8()?;
        let length: u8 = (prefix_length + 7) / 8;
        let mut prefix: Vec<u8> = vec![0; length as usize];
        stream.read_exact(&mut prefix)?;

        // Read the attributes
        let attribute_length = stream.read_u16::<BigEndian>()?;
        let mut attributes = vec![0; attribute_length as usize];
        stream.read_exact(&mut attributes)?;

        Ok(ENTRY {
            peer_as,
            local_as,
            interface,
            peer_address,
            local_address,
            view_number,
            status,
            time_last_change,
            next_hop,
            afi,
            safi,
            prefix_length,
            prefix,
            attributes,
        })
    }
}

impl BGP4MP {
    ///
    /// # Summary
    /// Used to parse sub-types of the BGP4MP MRT record type.
    ///
    /// # Panics
    /// This function does not panic.
    ///
    /// # Errors
    /// Any IO error will be returned while reading from the stream.
    /// If an ill-formatted stream or header is provided behavior will be undefined.
    ///
    /// # Safety
    /// This function does not make use of unsafe code.
    ///
    pub(crate) fn parse(header: &Header, stream: &mut Read) -> Result<BGP4MP, Error> {
        debug_assert!(
            header.record_type == 16 || header.record_type == 17,
            "Invalid record type in MRTHeader, expected BGP4MP record type."
        );

        match header.sub_type {
            0 => Ok(BGP4MP::STATE_CHANGE(STATE_CHANGE::parse(stream)?)),
            1 => Ok(BGP4MP::MESSAGE(MESSAGE::parse(header, stream)?)),
            2 => Ok(BGP4MP::ENTRY(ENTRY::parse(stream)?)),
            3 => Ok(BGP4MP::SNAPSHOT(SNAPSHOT::parse(stream)?)),
            4 => Ok(BGP4MP::MESSAGE_AS4(MESSAGE_AS4::parse(header, stream)?)),
            5 => Ok(BGP4MP::STATE_CHANGE_AS4(STATE_CHANGE_AS4::parse(stream)?)),
            6 => Ok(BGP4MP::MESSAGE_LOCAL(MESSAGE::parse(header, stream)?)),
            7 => Ok(BGP4MP::MESSAGE_AS4_LOCAL(MESSAGE_AS4::parse(
                header, stream,
            )?)),
            8 => Ok(BGP4MP::MESSAGE_ADDPATH(MESSAGE::parse(header, stream)?)),
            9 => Ok(BGP4MP::MESSAGE_AS4_ADDPATH(MESSAGE_AS4::parse(
                header, stream,
            )?)),
            10 => Ok(BGP4MP::MESSAGE_LOCAL_ADDPATH(MESSAGE::parse(
                header, stream,
            )?)),
            11 => Ok(BGP4MP::MESSAGE_AS4_LOCAL_ADDPATH(MESSAGE_AS4::parse(
                header, stream,
            )?)),
            _ => Err(Error::new(
                ErrorKind::InvalidData,
                "Unknown MRT record subtype found in MRTHeader",
            )),
        }
    }
}