Skip to main content

bgpkit_parser/models/bgp/
evpn.rs

1//! Ethernet VPN (EVPN) NLRI and route fields - RFC 7432.
2//!
3//! EVPN is carried in the L2VPN address family ([`Afi::L2vpn`]) with the EVPN
4//! subsequent address family ([`Safi::Evpn`]). Every NLRI starts with a route
5//! type and a length in octets, followed by a route-distinguisher and a
6//! type-specific body.
7//!
8//! References:
9//! - RFC 7432: BGP MPLS-Based Ethernet VPN - route types 1 to 4, MAC Mobility
10//!   extended community (§7.2, §7.3, §7.7)
11//! - RFC 8365: Network Virtualization Overlay Solution Using EVPN - VXLAN
12//!   encapsulation, where the label field carries the VXLAN Network Identifier
13//! - RFC 9136: IP Prefix Advertisement in EVPN - route type 5 (§3.1)
14//!
15//! [`Afi::L2vpn`]: crate::models::Afi::L2vpn
16//! [`Safi::Evpn`]: crate::models::Safi::Evpn
17
18use crate::models::RouteDistinguisher;
19use num_enum::{FromPrimitive, IntoPrimitive};
20use std::net::IpAddr;
21
22/// EVPN route type - the first octet of every EVPN NLRI.
23///
24/// Types 1 to 5 are defined by RFC 7432 and RFC 9136; anything else is kept as
25/// [`EvpnRouteType::Unknown`] so that new route types do not fail parsing.
26#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, IntoPrimitive)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28#[repr(u8)]
29pub enum EvpnRouteType {
30    /// Ethernet Auto-Discovery route - RFC 7432 §7.1
31    EthernetAutoDiscovery = 1,
32    /// MAC/IP Advertisement route - RFC 7432 §7.2
33    MacIpAdvertisement = 2,
34    /// Inclusive Multicast Ethernet Tag route - RFC 7432 §7.3
35    InclusiveMulticastEthernetTag = 3,
36    /// Ethernet Segment route - RFC 7432 §7.4
37    EthernetSegment = 4,
38    /// IP Prefix route - RFC 9136 §3.1
39    IpPrefix = 5,
40    #[num_enum(catch_all)]
41    Unknown(u8),
42}
43
44/// One EVPN NLRI entry (AFI 25 / SAFI 70).
45#[derive(Debug, PartialEq, Eq, Clone)]
46#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
47pub struct EvpnNlri {
48    /// ADD-PATH path identifier (RFC 7911); present when the session
49    /// negotiated ADD-PATH, `None` otherwise.
50    pub path_id: Option<u32>,
51    pub route_type: EvpnRouteType,
52    /// Route Distinguisher (8 octets), shared by every EVPN route type.
53    pub route_distinguisher: RouteDistinguisher,
54    pub payload: EvpnRoutePayload,
55}
56
57/// Route-type-specific part of an EVPN NLRI.
58#[derive(Debug, PartialEq, Eq, Clone)]
59#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
60pub enum EvpnRoutePayload {
61    /// Route type 2 - RFC 7432 §7.2
62    MacIpAdvertisement(MacIpAdvertisement),
63    /// Route type 3 - RFC 7432 §7.3
64    InclusiveMulticast(InclusiveMulticastRoute),
65    /// Route type 5 - RFC 9136 §3.1
66    IpPrefix(IpPrefixRoute),
67    /// Route types this parser does not decode yet (1 and 4 at the time of
68    /// writing). The octets after the route distinguisher are preserved, so
69    /// nothing is lost and the value survives a debug or JSON round trip.
70    Raw(Vec<u8>),
71}
72
73/// MAC/IP Advertisement route body - RFC 7432 §7.2.
74#[derive(Debug, PartialEq, Eq, Clone)]
75#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
76pub struct MacIpAdvertisement {
77    /// Ethernet Segment Identifier (10 octets); all zero for a single-homed
78    /// host - RFC 7432 §5.
79    pub esi: [u8; 10],
80    /// Ethernet Tag ID (4 octets).
81    pub ethernet_tag_id: u32,
82    /// MAC address (6 octets); the route type fixes the length at 48 bits.
83    pub mac: [u8; 6],
84    /// IP address when the advertisement carries one; `None` when the IP
85    /// address length is zero, which is a MAC-only advertisement.
86    pub ip: Option<IpAddr>,
87    /// Label1 as it appears on the wire: a 24-bit field. For VXLAN this *is*
88    /// the VNI, with no MPLS label semantics - RFC 8365 §5; an MPLS-signalled
89    /// fabric puts a label plus its TC/S/TTL bits here instead, see
90    /// [`MacIpAdvertisement::mpls_label`].
91    pub raw_label: u32,
92    /// Optional Label2, same encoding as [`Self::raw_label`], present when the
93    /// route is used for IP-VRF forwarding - RFC 7432 §7.2.
94    pub raw_label2: Option<u32>,
95}
96
97impl MacIpAdvertisement {
98    /// Interpret Label1 as an MPLS label: the high 20 bits are the label value
99    /// and the low 4 bits carry TC, S and TTL - RFC 7432 §7.2.
100    ///
101    /// A VXLAN fabric does not use MPLS label semantics; there the whole
102    /// 24-bit field is the VNI and this value is meaningless.
103    pub fn mpls_label(&self) -> crate::models::MplsLabel {
104        crate::models::MplsLabel::decode([
105            (self.raw_label >> 16) as u8,
106            (self.raw_label >> 8) as u8,
107            self.raw_label as u8,
108        ])
109        .0
110    }
111}
112
113/// Inclusive Multicast Ethernet Tag route body - RFC 7432 §7.3.
114#[derive(Debug, PartialEq, Eq, Clone)]
115#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
116pub struct InclusiveMulticastRoute {
117    /// Ethernet Tag ID (4 octets).
118    pub ethernet_tag_id: u32,
119    /// Originating router's IP address (4 or 16 octets).
120    pub originating_router_ip: IpAddr,
121}
122
123/// IP Prefix route body - RFC 9136 §3.1.
124///
125/// The wire format carries the Ethernet Segment Identifier like the other EVPN
126/// route types, and the IP prefix and gateway address are always full width
127/// (4 or 16 octets). The NLRI length, not the prefix length, tells IPv4 (34
128/// octets) from IPv6 (58).
129#[derive(Debug, PartialEq, Eq, Clone)]
130#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
131pub struct IpPrefixRoute {
132    /// Ethernet Segment Identifier (10 octets).
133    pub esi: [u8; 10],
134    /// Ethernet Tag ID (4 octets).
135    pub ethernet_tag_id: u32,
136    /// The advertised IP prefix; the prefix length is on the wire and the
137    /// address family follows from it.
138    pub ip_prefix: ipnet::IpNet,
139    /// Gateway IP address (4 or 16 octets, matching the prefix family).
140    pub gateway_ip: IpAddr,
141    /// The 24-bit label field: a VNI in a VXLAN fabric (RFC 8365 §5), or an
142    /// MPLS label with TC/S/TTL in the low bits, see
143    /// [`IpPrefixRoute::mpls_label`].
144    pub raw_label: u32,
145}
146
147impl IpPrefixRoute {
148    /// Interpret the label field as an MPLS label - RFC 7432 §7.2.
149    pub fn mpls_label(&self) -> crate::models::MplsLabel {
150        crate::models::MplsLabel::decode([
151            (self.raw_label >> 16) as u8,
152            (self.raw_label >> 8) as u8,
153            self.raw_label as u8,
154        ])
155        .0
156    }
157}