bgpkit-parser 0.16.0

MRT/BGP/BMP data processing library
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
use crate::models::*;
use ipnet::IpNet;
use std::fmt::Debug;
use std::net::IpAddr;

/// Network Layer Reachability Information
#[derive(Debug, PartialEq, Clone, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Nlri {
    pub afi: Afi,
    pub safi: Safi,
    pub next_hop: Option<NextHopAddress>,
    /// Traditional IP prefixes for unicast/multicast
    pub prefixes: Vec<NetworkPrefix>,
    /// Link-State NLRI data - RFC 7752
    pub link_state_nlris: Option<Vec<crate::models::bgp::linkstate::LinkStateNlri>>,
    /// Flow-Spec NLRI data - RFC 8955/8956
    pub flowspec_nlris: Option<Vec<crate::models::bgp::flowspec::FlowSpecNlri>>,
}

impl Nlri {
    /// Returns true if this NLRI refers to the IPv4 address space.
    pub const fn is_ipv4(&self) -> bool {
        matches!(self.afi, Afi::Ipv4)
    }

    /// Returns true if this NLRI refers to the IPv6 address space.
    pub const fn is_ipv6(&self) -> bool {
        matches!(self.afi, Afi::Ipv6)
    }

    /// Returns true if this NLRI refers to Link-State information.
    pub const fn is_link_state(&self) -> bool {
        matches!(self.afi, Afi::LinkState)
    }

    /// Returns true if this NLRI refers to Flow-Spec information.
    pub const fn is_flowspec(&self) -> bool {
        matches!(self.safi, Safi::FlowSpec | Safi::FlowSpecL3Vpn)
    }

    /// Returns true if this NLRI refers to reachable prefixes
    pub const fn is_reachable(&self) -> bool {
        self.next_hop.is_some()
    }

    /// Get the address of the next hop indicated by this NLRI.
    ///
    /// Panics if used on a unreachable NLRI message (ie. there is no next hop).
    pub const fn next_hop_addr(&self) -> IpAddr {
        match self.next_hop {
            Some(next_hop) => next_hop.addr(),
            None => panic!("unreachable NLRI"),
        }
    }

    pub fn new_reachable(prefix: NetworkPrefix, next_hop: Option<IpAddr>) -> Nlri {
        let next_hop = next_hop.map(NextHopAddress::from);
        let afi = match prefix.prefix {
            IpNet::V4(_) => Afi::Ipv4,
            IpNet::V6(_) => Afi::Ipv6,
        };
        let safi = Safi::Unicast;
        Nlri {
            afi,
            safi,
            next_hop,
            prefixes: vec![prefix],
            link_state_nlris: None,
            flowspec_nlris: None,
        }
    }

    pub fn new_unreachable(prefix: NetworkPrefix) -> Nlri {
        let afi = match prefix.prefix {
            IpNet::V4(_) => Afi::Ipv4,
            IpNet::V6(_) => Afi::Ipv6,
        };
        let safi = Safi::Unicast;
        Nlri {
            afi,
            safi,
            next_hop: None,
            prefixes: vec![prefix],
            link_state_nlris: None,
            flowspec_nlris: None,
        }
    }

    pub fn new_link_state_reachable(
        next_hop: Option<IpAddr>,
        safi: Safi,
        nlri_list: Vec<crate::models::bgp::linkstate::LinkStateNlri>,
    ) -> Nlri {
        let next_hop = next_hop.map(NextHopAddress::from);
        Nlri {
            afi: Afi::LinkState,
            safi,
            next_hop,
            prefixes: Vec::new(),
            link_state_nlris: Some(nlri_list),
            flowspec_nlris: None,
        }
    }

    pub fn new_link_state_unreachable(
        safi: Safi,
        nlri_list: Vec<crate::models::bgp::linkstate::LinkStateNlri>,
    ) -> Nlri {
        Nlri {
            afi: Afi::LinkState,
            safi,
            next_hop: None,
            prefixes: Vec::new(),
            link_state_nlris: Some(nlri_list),
            flowspec_nlris: None,
        }
    }

    /// Create a new Flow-Spec reachable NLRI
    pub fn new_flowspec_reachable(
        afi: Afi,
        safi: Safi,
        next_hop: Option<IpAddr>,
        flowspec_nlris: Vec<crate::models::bgp::flowspec::FlowSpecNlri>,
    ) -> Nlri {
        let next_hop = next_hop.map(NextHopAddress::from);
        Nlri {
            afi,
            safi,
            next_hop,
            prefixes: Vec::new(),
            link_state_nlris: None,
            flowspec_nlris: Some(flowspec_nlris),
        }
    }

    /// Create a new Flow-Spec unreachable NLRI
    pub fn new_flowspec_unreachable(
        afi: Afi,
        safi: Safi,
        flowspec_nlris: Vec<crate::models::bgp::flowspec::FlowSpecNlri>,
    ) -> Nlri {
        Nlri {
            afi,
            safi,
            next_hop: None,
            prefixes: Vec::new(),
            link_state_nlris: None,
            flowspec_nlris: Some(flowspec_nlris),
        }
    }
}

impl IntoIterator for Nlri {
    type Item = IpNet;
    type IntoIter = std::vec::IntoIter<IpNet>;

    fn into_iter(self) -> Self::IntoIter {
        self.prefixes
            .into_iter()
            .map(|x| x.prefix)
            .collect::<Vec<_>>()
            .into_iter()
    }
}

impl<'a> IntoIterator for &'a Nlri {
    type Item = &'a IpNet;
    type IntoIter = std::vec::IntoIter<&'a IpNet>;

    fn into_iter(self) -> Self::IntoIter {
        self.prefixes
            .iter()
            .map(|x| &x.prefix)
            .collect::<Vec<_>>()
            .into_iter()
    }
}

#[derive(Debug, PartialEq, Clone, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MpReachableNlri {
    afi: Afi,
    safi: Safi,
    next_hop: NextHopAddress,
    prefixes: Vec<NetworkPrefix>,
}

impl MpReachableNlri {
    pub fn new(
        afi: Afi,
        safi: Safi,
        next_hop: NextHopAddress,
        prefixes: Vec<NetworkPrefix>,
    ) -> MpReachableNlri {
        MpReachableNlri {
            afi,
            safi,
            next_hop,
            prefixes,
        }
    }
}

#[derive(Debug, PartialEq, Clone, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MpUnreachableNlri {
    afi: Afi,
    safi: Safi,
    prefixes: Vec<NetworkPrefix>,
}

impl MpUnreachableNlri {
    pub fn new(afi: Afi, safi: Safi, prefixes: Vec<NetworkPrefix>) -> MpUnreachableNlri {
        MpUnreachableNlri {
            afi,
            safi,
            prefixes,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::str::FromStr;

    #[test]
    fn nlri_is_ipv4() {
        let nlri = Nlri::new_reachable(
            NetworkPrefix::from_str("10.0.2.0/24").unwrap(),
            Some("10.0.2.1".parse().unwrap()),
        );

        assert!(nlri.is_ipv4());
    }

    #[test]
    fn nlri_is_ipv6() {
        let nlri = Nlri::new_unreachable(NetworkPrefix::from_str("2001:db8::/32").unwrap());

        assert!(nlri.is_ipv6());
    }

    #[test]
    fn nlri_is_reachable() {
        let nlri = Nlri::new_reachable(
            NetworkPrefix::from_str("10.0.2.0/24").unwrap(),
            Some("10.0.2.1".parse().unwrap()),
        );

        assert!(nlri.is_reachable());
    }

    #[test]
    #[should_panic]
    fn nlri_next_hop_addr_unreachable() {
        let nlri = Nlri::new_unreachable(NetworkPrefix::from_str("10.0.2.0/24").unwrap());

        let _ = nlri.next_hop_addr();
    }

    #[test]
    fn mp_reachable_nlri_new() {
        let next_hop_addr = IpAddr::from_str("10.0.2.1").unwrap();
        let nlri = MpReachableNlri::new(
            Afi::Ipv4,
            Safi::Unicast,
            NextHopAddress::from(next_hop_addr),
            vec![NetworkPrefix::from_str("10.0.2.0/24").unwrap()],
        );

        assert_eq!(nlri.afi, Afi::Ipv4);
        assert_eq!(nlri.safi, Safi::Unicast);
        assert_eq!(nlri.next_hop.addr(), next_hop_addr);
        assert_eq!(nlri.prefixes.len(), 1);
    }

    #[test]
    fn mp_unreachable_nlri_new() {
        let nlri = MpUnreachableNlri::new(
            Afi::Ipv4,
            Safi::Unicast,
            vec![NetworkPrefix::from_str("10.0.2.0/24").unwrap()],
        );

        assert_eq!(nlri.afi, Afi::Ipv4);
        assert_eq!(nlri.safi, Safi::Unicast);
        assert_eq!(nlri.prefixes.len(), 1);
    }

    #[test]
    fn nlri_link_state_creation() {
        use crate::models::bgp::linkstate::{LinkStateNlri, NodeDescriptor, ProtocolId};

        let node_desc = NodeDescriptor {
            autonomous_system: Some(65001),
            ..Default::default()
        };

        let ls_nlri = LinkStateNlri::new_node_nlri(ProtocolId::Ospfv2, 123456, node_desc);
        let nlri = Nlri::new_link_state_reachable(
            Some("192.168.1.1".parse().unwrap()),
            Safi::LinkState,
            vec![ls_nlri],
        );

        assert!(nlri.is_link_state());
        assert!(nlri.is_reachable());
        assert_eq!(nlri.afi, Afi::LinkState);
        assert_eq!(nlri.safi, Safi::LinkState);
    }

    #[test]
    fn nlri_link_state_unreachable() {
        use crate::models::bgp::linkstate::{LinkStateNlri, NodeDescriptor, ProtocolId};

        let node_desc = NodeDescriptor::default();
        let ls_nlri = LinkStateNlri::new_node_nlri(ProtocolId::Ospfv2, 123456, node_desc);
        let nlri = Nlri::new_link_state_unreachable(Safi::LinkState, vec![ls_nlri]);

        assert!(nlri.is_link_state());
        assert!(!nlri.is_reachable());
        assert_eq!(nlri.afi, Afi::LinkState);
        assert_eq!(nlri.safi, Safi::LinkState);
    }

    #[test]
    #[should_panic]
    fn nlri_link_state_next_hop_addr_unreachable() {
        use crate::models::bgp::linkstate::{LinkStateNlri, NodeDescriptor, ProtocolId};

        let node_desc = NodeDescriptor::default();
        let ls_nlri = LinkStateNlri::new_node_nlri(ProtocolId::Ospfv2, 123456, node_desc);
        let nlri = Nlri::new_link_state_unreachable(Safi::LinkState, vec![ls_nlri]);

        let _ = nlri.next_hop_addr();
    }

    #[test]
    fn nlri_flowspec_creation() {
        use crate::models::bgp::flowspec::{FlowSpecComponent, FlowSpecNlri};
        use std::str::FromStr;

        let component =
            FlowSpecComponent::DestinationPrefix(NetworkPrefix::from_str("192.0.2.0/24").unwrap());
        let flowspec_nlri = FlowSpecNlri::new(vec![component]);

        let nlri = Nlri::new_flowspec_reachable(
            Afi::Ipv4,
            Safi::FlowSpec,
            Some("192.0.2.1".parse().unwrap()),
            vec![flowspec_nlri],
        );

        assert!(nlri.is_flowspec());
        assert!(nlri.is_reachable());
        assert!(nlri.is_ipv4());
        assert_eq!(nlri.afi, Afi::Ipv4);
        assert_eq!(nlri.safi, Safi::FlowSpec);
        assert_eq!(nlri.prefixes.len(), 0); // Flow-Spec doesn't use traditional prefixes
        assert!(nlri.flowspec_nlris.is_some());
        assert_eq!(nlri.flowspec_nlris.as_ref().unwrap().len(), 1);
    }

    #[test]
    fn nlri_flowspec_unreachable() {
        use crate::models::bgp::flowspec::{FlowSpecComponent, FlowSpecNlri};
        use std::str::FromStr;

        let component =
            FlowSpecComponent::DestinationPrefix(NetworkPrefix::from_str("2001:db8::/32").unwrap());
        let flowspec_nlri = FlowSpecNlri::new(vec![component]);

        let nlri = Nlri::new_flowspec_unreachable(Afi::Ipv6, Safi::FlowSpec, vec![flowspec_nlri]);

        assert!(nlri.is_flowspec());
        assert!(!nlri.is_reachable());
        assert!(nlri.is_ipv6());
        assert_eq!(nlri.afi, Afi::Ipv6);
        assert_eq!(nlri.safi, Safi::FlowSpec);
        assert!(nlri.flowspec_nlris.is_some());
        assert_eq!(nlri.flowspec_nlris.as_ref().unwrap().len(), 1);
    }

    #[test]
    fn nlri_flowspec_l3vpn() {
        use crate::models::bgp::flowspec::{FlowSpecComponent, FlowSpecNlri, NumericOperator};
        use std::str::FromStr;

        let components = vec![
            FlowSpecComponent::DestinationPrefix(NetworkPrefix::from_str("10.0.0.0/8").unwrap()),
            FlowSpecComponent::IpProtocol(vec![NumericOperator::equal_to(6)]), // TCP
        ];
        let flowspec_nlri = FlowSpecNlri::new(components);

        let nlri = Nlri::new_flowspec_reachable(
            Afi::Ipv4,
            Safi::FlowSpecL3Vpn,
            None, // Flow-Spec often doesn't have next hop
            vec![flowspec_nlri],
        );

        assert!(nlri.is_flowspec());
        assert!(!nlri.is_reachable());
        assert!(nlri.is_ipv4());
        assert_eq!(nlri.safi, Safi::FlowSpecL3Vpn);
    }

    #[test]
    #[should_panic]
    fn nlri_flowspec_next_hop_addr_unreachable() {
        use crate::models::bgp::flowspec::{FlowSpecComponent, FlowSpecNlri};
        use std::str::FromStr;

        let component =
            FlowSpecComponent::DestinationPrefix(NetworkPrefix::from_str("192.0.2.0/24").unwrap());
        let flowspec_nlri = FlowSpecNlri::new(vec![component]);

        let nlri = Nlri::new_flowspec_unreachable(Afi::Ipv4, Safi::FlowSpec, vec![flowspec_nlri]);

        let _ = nlri.next_hop_addr();
    }
}