bgp-rs 0.6.0

A library for parsing Border Gateway Protocol (BGP) formatted streams.
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
/// Contains the implementation of all BGP path attributes.
pub mod attributes;
pub use crate::attributes::*;
/// Contains the implementation of BGP NLRI.
pub mod nlri;
pub use crate::nlri::*;
#[cfg(feature = "flowspec")]
/// Contains the implementation of Flowspec attributes
pub mod flowspec;
#[cfg(feature = "flowspec")]
pub use crate::flowspec::*;

use crate::*;

use std::collections::HashMap;
use std::io::{Cursor, Error, Read};
use std::net::IpAddr;

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

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

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

impl Update {
    /// docs
    pub fn parse(
        header: &Header,
        stream: &mut impl Read,
        capabilities: &Capabilities,
    ) -> Result<Update, Error> {
        if header.length < 23 {
            return Err(Error::new(
                ErrorKind::Other,
                format!("Header had bogus length {} < 23", header.length),
            ));
        }
        let mut nlri_length: usize = header.length as usize - 23;

        // ----------------------------
        // Read withdrawn routes.
        // ----------------------------
        let withdraw_len = stream.read_u16::<BigEndian>()? as usize;
        if withdraw_len > nlri_length {
            return Err(Error::new(
                ErrorKind::Other,
                format!(
                    "Got bogus withdraw length {} < msg len {}",
                    withdraw_len, nlri_length
                ),
            ));
        }
        let mut buffer = vec![0; withdraw_len];
        stream.read_exact(&mut buffer)?;
        nlri_length -= withdraw_len;

        let mut withdrawn_routes: Vec<NLRIEncoding> = Vec::with_capacity(0);
        let mut cursor = Cursor::new(buffer);

        if capabilities.EXTENDED_PATH_NLRI_SUPPORT {
            while cursor.position() < withdraw_len as u64 {
                let path_id = cursor.read_u32::<BigEndian>()?;
                let prefix = Prefix::parse(&mut cursor, AFI::IPV4)?;
                withdrawn_routes.push(NLRIEncoding::IP_WITH_PATH_ID((prefix, path_id)));
            }
        } else {
            while cursor.position() < withdraw_len as u64 {
                withdrawn_routes.push(NLRIEncoding::IP(Prefix::parse(&mut cursor, AFI::IPV4)?));
            }
        }

        // ----------------------------
        // Read path attributes
        // ----------------------------
        let length = stream.read_u16::<BigEndian>()? as usize;
        if length > nlri_length {
            return Err(Error::new(
                ErrorKind::Other,
                format!(
                    "Got bogus attributes length {} < msg len {} - withdraw len {}",
                    length, nlri_length, withdraw_len
                ),
            ));
        }
        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 = match PathAttribute::parse(&mut cursor, capabilities) {
                Ok(a) => a,
                Err(e) => match e.kind() {
                    ErrorKind::UnexpectedEof => return Err(e),
                    _ => continue,
                },
            };
            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<NLRIEncoding> = Vec::with_capacity(4);

        while cursor.position() < nlri_length as u64 {
            if util::detect_add_path_prefix(&mut cursor, 32)? {
                let path_id = cursor.read_u32::<BigEndian>()?;
                let prefix = Prefix::parse(&mut cursor, AFI::IPV4)?;
                announced_routes.push(NLRIEncoding::IP_WITH_PATH_ID((prefix, path_id)));
            } else {
                announced_routes.push(NLRIEncoding::IP(Prefix::parse(&mut cursor, AFI::IPV4)?));
            }
        }

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

    /// Update message to bytes
    pub fn encode(&self, buf: &mut impl Write) -> Result<(), Error> {
        // Create one buf to reuse for each Update attribute
        let mut temp_buf: Vec<u8> = Vec::with_capacity(8);

        let mut unreach_nlri: HashMap<(AFI, SAFI), Vec<NLRIEncoding>> = HashMap::new();
        for withdrawal in &self.withdrawn_routes {
            if withdrawal.is_ipv4() {
                withdrawal.encode(&mut temp_buf)?;
            } else {
                // Encode into MP_UNREACH_NLRI
                let nlris = unreach_nlri
                    .entry((withdrawal.afi(), withdrawal.safi()))
                    .or_insert_with(Vec::new);
                nlris.push(withdrawal.clone());
            }
        }
        buf.write_u16::<BigEndian>(temp_buf.len() as u16)?;
        buf.write_all(&temp_buf)?;
        temp_buf.clear();

        // Path Attributes
        for attribute in &self.attributes {
            attribute.encode(&mut temp_buf)?;
        }
        for ((afi, safi), unreach_nlris) in unreach_nlri.into_iter() {
            let pa = PathAttribute::MP_UNREACH_NLRI(MPUnreachNLRI {
                afi,
                safi,
                withdrawn_routes: unreach_nlris,
            });
            pa.encode(&mut temp_buf)?;
        }
        buf.write_u16::<BigEndian>(temp_buf.len() as u16)?;
        buf.write_all(&temp_buf)?;
        temp_buf.clear();

        // NLRI
        for route in &self.announced_routes {
            route.encode(&mut temp_buf)?;
        }
        buf.write_all(&temp_buf)
    }

    /// 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.
        let identifier = match self.get(Identifier::MP_REACH_NLRI) {
            Some(PathAttribute::MP_REACH_NLRI(routes)) => Some(routes.announced_routes.clone()),
            _ => None,
        };
        if let Some(routes) = identifier {
            self.announced_routes.extend(routes)
        }

        // Move the MP_REACH_NLRI attribute in the NLRI.
        let identifier = match self.get(Identifier::MP_UNREACH_NLRI) {
            Some(PathAttribute::MP_UNREACH_NLRI(routes)) => Some(routes.withdrawn_routes.clone()),
            _ => None,
        };
        if let Some(routes) = identifier {
            self.withdrawn_routes.extend(routes)
        }
    }
}

/// Represents NLRIEncodings present in the NRLI section of an UPDATE message.
#[derive(Debug, Clone, Eq, PartialEq)]
#[allow(non_camel_case_types)]
pub enum NLRIEncoding {
    /// Encodings that specify only an IP present, either IPv4 or IPv6
    IP(Prefix),

    /// Encodings that specify a Path Identifier as specified in RFC7911. (Prefix, Path ID)
    IP_WITH_PATH_ID((Prefix, u32)),

    /// Encodings with a labeled nexthop as specified in RFC8277. (Prefix, MPLS Label)
    IP_MPLS((Prefix, u32)),

    /// Encodings with a labeled nexthop as specified in RFC8277. (Prefix, MPLS Label, Path ID)
    IP_MPLS_WITH_PATH_ID((Prefix, u32, u32)),

    /// Encodings for VPNs with a labeled nexthop as specified in RFC8277. (Prefix, MPLS Label)
    IP_VPN_MPLS((u64, Prefix, u32)),

    /// Encodings that specify a VPLS endpoint as specified in RFC4761. (RD, VE ID, Label Block Offset, Label Block Size, Label Base)
    L2VPN((u64, u16, u16, u16, u32)),

    /// Flowspec Traffic Filter Specification - RFC5575
    #[cfg(feature = "flowspec")]
    FLOWSPEC(Vec<FlowspecFilter>),
}

impl NLRIEncoding {
    /// Check if this is a normal IPv4 NLRI for Update encoding
    pub fn is_ipv4(&self) -> bool {
        if let NLRIEncoding::IP(prefix) = &self {
            prefix.protocol == AFI::IPV4
        } else {
            false
        }
    }

    /// Derive the AFI for this NLRI
    pub fn afi(&self) -> AFI {
        use NLRIEncoding::*;
        match &self {
            IP(prefix) => prefix.protocol,
            #[cfg(feature = "flowspec")]
            FLOWSPEC(_) => AFI::IPV4, // TODO: match ipv6 from filters
            _ => unimplemented!(),
        }
    }

    /// Derive the SAFI for this NLRI
    pub fn safi(&self) -> SAFI {
        use NLRIEncoding::*;
        match &self {
            IP(_) => SAFI::Unicast,
            #[cfg(feature = "flowspec")]
            FLOWSPEC(_) => SAFI::Flowspec,
            _ => unimplemented!(),
        }
    }

    /// Encode NLRI to bytes
    pub fn encode(&self, buf: &mut impl Write) -> Result<(), Error> {
        match self {
            NLRIEncoding::IP(prefix) => {
                buf.write_u8(prefix.length)?;
                buf.write_all(&prefix.masked_octets())
            }
            NLRIEncoding::IP_WITH_PATH_ID((prefix, path_id)) => {
                buf.write_u32::<BigEndian>(*path_id)?;
                buf.write_u8(prefix.length)?;
                buf.write_all(&prefix.masked_octets())
            }
            NLRIEncoding::IP_VPN_MPLS((rd, prefix, label)) => {
                // TODO: the parsing in nlri.rs may not be correct
                buf.write_u32::<BigEndian>(*label)?;
                buf.write_u64::<BigEndian>(*rd)?;
                buf.write_all(&prefix.prefix)
            }
            #[cfg(feature = "flowspec")]
            NLRIEncoding::FLOWSPEC(filters) => {
                let mut bytes: Vec<u8> = Vec::with_capacity(16);
                for filter in filters {
                    filter.encode(&mut bytes)?;
                }
                buf.write_u8(bytes.len() as u8)?;
                buf.write_all(&bytes)
            }
            _ => unimplemented!("{:?}", self),
        }
    }
}

/// Represents a generic prefix. For example an IPv4 prefix or IPv6 prefix.
#[derive(Clone, Eq, PartialEq)]
pub struct Prefix {
    /// IP version for prefix (v4|v6)
    pub protocol: AFI,
    /// Prefix Mask length in bits
    pub length: u8,
    /// Prefix Octets
    pub 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)
            }
            AFI::L2VPN => unimplemented!(),
            AFI::BGPLS => unimplemented!(),
        }
    }
}

impl From<&Prefix> for (IpAddr, u8) {
    /// Convert from IpAddr/CIDR to Prefix
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr};
    /// use bgp_rs::Prefix;
    /// let prefix: Prefix = ("5.5.5.5".parse().unwrap(), 32).into();
    /// let (addr, length) = (&prefix).into();
    /// assert_eq!(addr, IpAddr::from(Ipv4Addr::new(5, 5, 5, 5)));
    /// assert_eq!(length, 32);
    /// ```
    fn from(prefix: &Prefix) -> (IpAddr, u8) {
        (IpAddr::from(prefix), prefix.length)
    }
}

impl From<(IpAddr, u8)> for Prefix {
    /// Convert from IpAddr/CIDR to Prefix
    /// ```
    /// use bgp_rs::Prefix;
    /// let prefix: Prefix = ("5.5.5.5".parse().unwrap(), 32).into();
    /// assert_eq!(prefix.length, 32);
    /// assert_eq!(prefix.prefix, vec![5, 5, 5, 5]);
    /// ```
    fn from(prefix: (IpAddr, u8)) -> Prefix {
        let (protocol, octets) = match prefix.0 {
            IpAddr::V4(v4) => (AFI::IPV4, v4.octets().to_vec()),
            IpAddr::V6(v6) => (AFI::IPV6, v6.octets().to_vec()),
        };
        Prefix {
            protocol,
            length: prefix.1,
            prefix: octets,
        }
    }
}

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 new(protocol: AFI, length: u8, prefix: Vec<u8>) -> Self {
        Self {
            protocol,
            length,
            prefix,
        }
    }

    fn octet_length(&self) -> usize {
        (self.length as usize + 7) / 8
    }

    /// Get a slice of the prefix octets covered by the prefix mask
    /// Useful for encoding the prefix in NLRI
    pub fn masked_octets(&self) -> &[u8] {
        &self.prefix[..self.octet_length()]
    }

    fn parse(stream: &mut impl Read, protocol: AFI) -> Result<Prefix, Error> {
        let length = stream.read_u8()?;

        if length
            > match protocol {
                AFI::IPV4 => 32,
                AFI::IPV6 => 128,
                AFI::L2VPN => unimplemented!(),
                AFI::BGPLS => unimplemented!(),
            }
        {
            return Err(Error::new(
                ErrorKind::Other,
                format!("Bogus prefix length {}", length),
            ));
        }

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

        Ok(Prefix {
            protocol,
            length,
            prefix,
        })
    }
}

#[test]
fn test_prefix_masked_octets() {
    let prefix = Prefix::new(AFI::IPV4, 32, vec![1, 1, 1, 1]);
    assert_eq!(prefix.masked_octets(), &[1, 1, 1, 1]);
    assert_eq!(&prefix.to_string(), "1.1.1.1/32");

    let prefix = Prefix::new(AFI::IPV4, 16, vec![1, 1, 1, 1]);
    assert_eq!(prefix.masked_octets(), &[1, 1]);
    assert_eq!(&prefix.to_string(), "1.1.1.1/16");

    let prefix = Prefix::new(AFI::IPV4, 18, vec![1, 1, 1, 1]);
    assert_eq!(prefix.masked_octets(), &[1, 1, 1]);
    assert_eq!(&prefix.to_string(), "1.1.1.1/18");
}