diff --git a/holo-bgp/src/events.rs b/holo-bgp/src/events.rs index 176f8cd0..ab8d1bb8 100644 --- a/holo-bgp/src/events.rs +++ b/holo-bgp/src/events.rs @@ -9,7 +9,7 @@ use std::net::IpAddr; use chrono::Utc; use holo_protocol::InstanceShared; -use holo_utils::bgp::RouteType; +use holo_utils::bgp::{RouteType, WellKnownCommunities}; use holo_utils::ip::{IpAddrKind, IpNetworkKind}; use holo_utils::policy::{PolicyResult, PolicyType}; use holo_utils::socket::{TcpConnInfo, TcpStream}; @@ -728,6 +728,28 @@ pub(crate) fn advertise_routes( return false; } + // Handle well-known communities. + if let Some(comm) = &route.attrs.comm { + for comm in comm + .value + .iter() + .filter_map(|comm| WellKnownCommunities::from_u32(comm.0)) + { + // Do not advertise to any other peer. + if comm == WellKnownCommunities::NoAdvertise { + return false; + } + + // Do not advertise to external peers. + if nbr.peer_type == PeerType::External + && (comm == WellKnownCommunities::NoExport + || comm == WellKnownCommunities::NoExportSubconfed) + { + return false; + } + } + } + true }); diff --git a/holo-bgp/src/packet/attribute.rs b/holo-bgp/src/packet/attribute.rs index aa9c99bd..e901a758 100644 --- a/holo-bgp/src/packet/attribute.rs +++ b/holo-bgp/src/packet/attribute.rs @@ -1400,6 +1400,10 @@ impl CommList { fn length(&self) -> u16 { ATTR_MIN_LEN_EXT + (self.0.len() * T::LENGTH) as u16 } + + pub(crate) fn iter(&self) -> impl Iterator { + self.0.iter() + } } // ===== helper functions =====