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
#![deny(missing_docs)]

//! The `bgp-rs` crate provides functionality to parse BGP-formatted streams.
//!
//! # Examples
//!
//! ## Reading a MRT file containing BPG messages
//! ```
//! use std::fs::File;
//! use std::io::Cursor;
//! use std::io::Read;
//! use std::io::BufReader;
//! use mrt_rs::Record;
//! use mrt_rs::bgp4mp::BGP4MP;
//! use libflate::gzip::Decoder;
//! use bgp_rs::{Identifier, PathAttribute};
//!
//! fn main() {
//!    // Download an update message.
//!    let file = File::open("res/updates.20190101.0000.gz").unwrap();
//!
//!    // Decode the GZIP stream.
//!    let decoder = Decoder::new(BufReader::new(file)).unwrap();
//!
//!    // Create a new MRTReader with a Cursor such that we can keep track of the position.
//!    let mut reader = mrt_rs::Reader { stream: decoder };
//!
//!    // Keep reading MRT (Header, Record) tuples till the end of the file has been reached.
//!    while let Ok(Some((_, record))) = reader.read() {
//!
//!        // Extract BGP4MP::MESSAGE_AS4 entries.
//!        if let Record::BGP4MP(BGP4MP::MESSAGE_AS4(x)) = record {
//!
//!            // Read each BGP (Header, Message)
//!            let cursor = Cursor::new(x.message);
//!            let mut reader = bgp_rs::Reader { stream: cursor };
//!            let (_, message) = reader.read().unwrap();
//!
//!            // If this is an UPDATE message that contains announcements, extract its origin.
//!            if let bgp_rs::Message::Update(x) = message {
//!                if x.is_announcement() {
//!                    if let PathAttribute::AS_PATH(path) = x.get(Identifier::AS_PATH).unwrap()
//!                    {
//!                        // Test the path.origin() method.
//!                        let origin = path.origin();
//!
//!                        // Do other stuff ...
//!                    }
//!                }
//!            }
//!        }
//!    }
//! }
//! ```

/// Contains the implementation of all BGP path attributes.
pub mod attributes;

use byteorder::{BigEndian, ReadBytesExt};
use std::io::{Cursor, Error, ErrorKind, Read};

pub use crate::attributes::*;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::net::IpAddr;

/// Represents an Address Family Idenfitier. Currently only IPv4 and IPv6 are supported.
#[derive(Debug, Copy, Clone)]
#[repr(u16)]
pub enum AFI {
    /// Internet Protocol version 4 (32 bits)
    IPV4 = 1,
    /// Internet Protocol version 6 (128 bits)
    IPV6 = 2,
}

impl AFI {
    fn from(value: u16) -> Result<AFI, Error> {
        match value {
            1 => Ok(AFI::IPV4),
            2 => Ok(AFI::IPV6),
            _ => {
                let msg = format!(
                    "Number {} does not represent a valid address family.",
                    value
                );
                Err(std::io::Error::new(std::io::ErrorKind::Other, msg))
            }
        }
    }
}

/// Represents the BGP header accompanying every BGP message.
#[derive(Debug)]
pub struct Header {
    /// Predefined marker, must be set to all ones.
    pub marker: [u8; 16],

    /// Indicates the total length of the message, including the header in bytes.
    pub length: u16,

    /// Indicates the type of message that follows the header.
    pub record_type: u8,
}

/// Represents a single BGP message.
#[derive(Debug)]
pub enum Message {
    /// Represent a BGP OPEN message.
    Open(Open),

    /// Represent a BGP UPDATE message.
    Update(Update),

    /// Represent a BGP NOTIFICATION message.
    Notification,

    /// Represent a BGP KEEPALIVE message.
    KeepAlive,

    /// Represent a BGP ROUTE_REFRESH message.
    RouteRefresh,
}

/// Represents a BGP Open message.
#[derive(Debug)]
pub struct Open {
    /// Indicates the protocol version number of the message. The current BGP version number is 4.
    version: u8,

    /// Indicates the Autonomous System number of the sender.
    peer_asn: u16,

    /// Indicates the number of seconds the sender proposes for the value of the Hold Timer.
    hold_timer: u16,

    /// Indicates the BGP Identifier of the sender.
    identifier: u32,

    /// Optional Parameters
    parameters: Vec<OpenParameter>,
}

impl Open {
    fn parse(stream: &mut Read) -> Result<Open, Error> {
        let version = stream.read_u8()?;
        let peer_asn = stream.read_u16::<BigEndian>()?;
        let hold_timer = stream.read_u16::<BigEndian>()?;
        let identifier = stream.read_u32::<BigEndian>()?;
        let length = stream.read_u8()?;
        let mut parameters: Vec<OpenParameter> = Vec::with_capacity(length as usize);
        for _ in 0..length {
            parameters.push(OpenParameter::parse(stream)?);
        }

        Ok(Open {
            version,
            peer_asn,
            hold_timer,
            identifier,
            parameters,
        })
    }
}

/// Represents a parameter in the optional parameter section of an Open message.
#[derive(Debug)]
pub struct OpenParameter {
    /// The type of the parameter.
    pub param_type: u8,

    /// The length of the data that this parameter holds in bytes.
    pub length: u8,

    /// The value that is set for this parameter.
    pub value: Vec<u8>,
}

impl OpenParameter {
    fn parse(stream: &mut Read) -> Result<OpenParameter, Error> {
        let param_type = stream.read_u8()?;
        let length = stream.read_u8()?;
        let mut value = vec![0; length as usize];
        stream.read_exact(&mut value)?;
        Ok(OpenParameter {
            param_type,
            length,
            value,
        })
    }
}

/// Represents a BGP Update message.
#[derive(Debug)]
pub struct Update {
    /// A collection of routes that have been withdrawn.
    withdrawn_routes: Vec<Prefix>,

    /// A collection of attributes associated with the announced routes.
    attributes: Vec<PathAttribute>,

    /// A collection of routes that are announced by the peer.
    announced_routes: Vec<Prefix>,
}

impl Update {
    fn parse(stream: &mut Read, header: &Header) -> Result<Update, Error> {
        let mut nlri_length: usize = header.length as usize - 23;

        // ----------------------------
        // Read withdrawn routes.
        // ----------------------------
        let length = stream.read_u16::<BigEndian>()? as usize;
        let mut buffer = vec![0; length];
        stream.read_exact(&mut buffer)?;
        nlri_length -= length;

        let mut withdrawn_routes: Vec<Prefix> = Vec::with_capacity(0);
        let mut cursor = Cursor::new(buffer);
        while cursor.position() < length as u64 {
            withdrawn_routes.push(Prefix::parse(&mut cursor, AFI::IPV4)?);
        }

        // ----------------------------
        // Read path attributes
        // ----------------------------
        let length = stream.read_u16::<BigEndian>()? as usize;
        let mut buffer = vec![0; length];
        stream.read_exact(&mut buffer)?;
        nlri_length -= length;

        let mut attributes: Vec<PathAttribute> = Vec::with_capacity(8);
        let mut cursor = Cursor::new(buffer);
        while cursor.position() < length as u64 {
            let attribute = PathAttribute::parse(&mut cursor)?;
            attributes.push(attribute);
        }

        // ----------------------------
        // Read NLRI
        // ----------------------------
        let mut buffer = vec![0; nlri_length as usize];
        stream.read_exact(&mut buffer)?;
        let mut cursor = Cursor::new(buffer);
        let mut announced_routes: Vec<Prefix> = Vec::with_capacity(4);

        while cursor.position() < nlri_length as u64 {
            announced_routes.push(Prefix::parse(&mut cursor, AFI::IPV4)?);
        }

        Ok(Update {
            withdrawn_routes,
            attributes,
            announced_routes,
        })
    }

    /// Retrieves the first PathAttribute that matches the given identifier.
    pub fn get(&self, identifier: Identifier) -> Option<&PathAttribute> {
        for a in &self.attributes {
            if a.id() == identifier {
                return Some(a);
            }
        }
        None
    }

    /// Checks if this UPDATE message contains announced prefixes.
    pub fn is_announcement(&self) -> bool {
        if !self.announced_routes.is_empty() || self.get(Identifier::MP_REACH_NLRI).is_some() {
            return true;
        }
        false
    }

    /// Checks if this UPDATE message contains withdrawn routes..
    pub fn is_withdrawal(&self) -> bool {
        if !self.withdrawn_routes.is_empty() || self.get(Identifier::MP_UNREACH_NLRI).is_some() {
            return true;
        }
        false
    }

    /// Moves the MP_REACH and MP_UNREACH NLRI into the NLRI.
    pub fn normalize(&mut self) {
        // Move the MP_REACH_NLRI attribute in the NLRI.
        if let Some(PathAttribute::MP_REACH_NLRI(routes)) = self.get(Identifier::MP_REACH_NLRI) {
            self.announced_routes
                .extend(routes.announced_routes.clone())
        }

        // Move the MP_REACH_NLRI attribute in the NLRI.
        if let Some(PathAttribute::MP_UNREACH_NLRI(routes)) = self.get(Identifier::MP_UNREACH_NLRI)
        {
            self.withdrawn_routes
                .extend(routes.withdrawn_routes.clone())
        }
    }
}

/// Represents a generic prefix. For example an IPv4 prefix or IPv6 prefix.
#[derive(Clone)]
pub struct Prefix {
    protocol: AFI,
    length: u8,
    prefix: Vec<u8>,
}

impl From<&Prefix> for IpAddr {
    fn from(prefix: &Prefix) -> Self {
        match prefix.protocol {
            AFI::IPV4 => {
                let mut buffer: [u8; 4] = [0; 4];
                buffer[..prefix.prefix.len()].clone_from_slice(&prefix.prefix[..]);
                IpAddr::from(buffer)
            }
            AFI::IPV6 => {
                let mut buffer: [u8; 16] = [0; 16];
                buffer[..prefix.prefix.len()].clone_from_slice(&prefix.prefix[..]);
                IpAddr::from(buffer)
            }
        }
    }
}

impl Display for Prefix {
    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
        write!(f, "{}/{}", IpAddr::from(self), self.length)
    }
}

impl Debug for Prefix {
    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
        write!(f, "{}/{}", IpAddr::from(self), self.length)
    }
}

impl Prefix {
    fn parse(stream: &mut Read, protocol: AFI) -> Result<Prefix, Error> {
        let length = stream.read_u8()?;
        let mut prefix: Vec<u8> = vec![0; ((length + 7) / 8) as usize];
        stream.read_exact(&mut prefix)?;
        Ok(Prefix {
            protocol,
            length,
            prefix,
        })
    }
}

/// Represents a BGP Notification message.
#[derive(Debug)]
pub struct Notification {}

/// The BGPReader can read BGP messages from a BGP-formatted stream.
pub struct Reader<T>
where
    T: Read,
{
    /// The stream from which BGP messages will be read.
    pub stream: T,
}

impl<T> Reader<T>
where
    T: Read,
{
    ///
    /// Reads the next BGP message in the stream.
    ///
    /// # Panics
    /// This function does not panic.
    ///
    /// # Errors
    /// Any IO error will be returned while reading from the stream.
    /// If an ill-formatted stream provided behavior will be undefined.
    ///
    /// # Safety
    /// This function does not make use of unsafe code.
    ///
    pub fn read(&mut self) -> Result<(Header, Message), Error> {
        // Parse the header.
        let mut marker: [u8; 16] = [0; 16];
        self.stream.read_exact(&mut marker)?;

        let header = Header {
            marker,
            length: self.stream.read_u16::<BigEndian>()?,
            record_type: self.stream.read_u8()?,
        };

        match header.record_type {
            1 => Ok((header, Message::Open(Open::parse(&mut self.stream)?))),
            2 => {
                let attribute = Message::Update(Update::parse(&mut self.stream, &header)?);
                Ok((header, attribute))
            }
            3 => Ok((header, Message::Notification)),
            4 => Ok((header, Message::KeepAlive)),
            5 => unimplemented!("ROUTE-REFRESH messages are not yet implemented."),
            _ => Err(Error::new(
                ErrorKind::Other,
                "Unknown BGP message type found in BGPHeader",
            )),
        }
    }
}