Skip to content

Commit

Permalink
Merge pull request #8 from bgpkit/extend-rfc-supports
Browse files Browse the repository at this point in the history
Add extended community support
  • Loading branch information
digizeph authored Nov 27, 2021
2 parents 08748ae + 4d5e55e commit 156eaf4
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bgp-models"
version = "0.4.1"
version = "0.5.0"
edition = "2018"
authors = ["Mingwei Zhang <mingwei@bgpkit.com>"]
readme = "README.md"
Expand Down
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,53 @@

[![Rust](https://github.com/bgpkit/bgp-models/actions/workflows/rust.yml/badge.svg)](https://github.com/bgpkit/bgp-models/actions/workflows/rust.yml)

Structs and other building blocks for BGP and MRT related Rust projects.
`bgp-models` is a library that defines the basic BGP and MRT message data structures.
This library aims to provide building blocks for downstreams libraries working with BGP and MRT
messages such as MRT parser or BGP table constructor.

## Supported RFCs

Most of the structs defined in this library are named after the formal definitions in a number of
RFCs. Here is a list of them:

### BGP
- [X] [RFC 4271](https://datatracker.ietf.org/doc/html/rfc4271): A Border Gateway Protocol 4 (BGP-4)
- [X] [RFC 6793](https://datatracker.ietf.org/doc/html/rfc6793): BGP Support for Four-Octet Autonomous System (AS) Number Space

### MRT

- [X] [RFC 6396](https://datatracker.ietf.org/doc/html/rfc6396): Multi-Threaded Routing Toolkit (MRT) Routing Information Export Format
- [ ] [RFC 6397](https://datatracker.ietf.org/doc/html/rfc6397): Multi-Threaded Routing Toolkit (MRT) Border Gateway Protocol (BGP) Routing Information Export Format with Geo-Location Extensions
- [X] [RFC 8050](https://datatracker.ietf.org/doc/html/rfc8050): Multi-Threaded Routing Toolkit (MRT) Routing Information Export Format with BGP Additional Path Extensions

### Communities

#### Communities

- [X] [RFC 1977](https://datatracker.ietf.org/doc/html/rfc1977): BGP Communities Attribute

#### Extended Communities

- [X] [RFC 4360](https://datatracker.ietf.org/doc/html/rfc4360): BGP Extended Communities Attribute
- [X] [RFC 5668](https://datatracker.ietf.org/doc/html/rfc5668): 4-Octet AS Specific BGP Extended Community
- [X] [RFC 5701](https://datatracker.ietf.org/doc/html/rfc5701): IPv6 Address Specific BGP Extended Community Attribute
- [X] [RFC 7153](https://datatracker.ietf.org/doc/html/rfc7153): IANA Registries for BGP Extended Communities Updates 4360, 5701
- [X] [RFC 8097](https://datatracker.ietf.org/doc/html/rfc8097): BGP Prefix Origin Validation State Extended Community

#### Large Communities

- [X] [RFC 8092](https://datatracker.ietf.org/doc/html/rfc8092): BGP Large Communities

#### Other Informational

- [RFC 4384](https://datatracker.ietf.org/doc/html/rfc4384): BGP Communities for Data Collection BCP 114
- [RFC 8195](https://datatracker.ietf.org/doc/html/rfc8195): Use of BGP Large Communities (informational)
- [RFC 8642](https://datatracker.ietf.org/doc/html/rfc8642): Policy Behavior for Well-Known BGP Communities

## Used By

- [bgpkit-parser](https://github.com/bgpkit/bgpkit-parser)
- [ris-live-rs](https://github.com/bgpkit/ris-live-rs)

## Built with ❤️ by BGPKIT Team

Expand Down
96 changes: 19 additions & 77 deletions src/bgp/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::net::IpAddr;
use itertools::Itertools;
use crate::network::*;
use serde::{Serialize, Serializer};
use crate::bgp::community::{Community, ExtendedCommunity, Ipv6AddressSpecificExtendedCommunity, LargeCommunity};

/// The high-order bit (bit 0) of the Attribute Flags octet is the
/// Optional bit. It defines whether the attribute is optional (if
Expand Down Expand Up @@ -40,26 +41,9 @@ pub enum AttributeFlagsBit {

/// Attribute types.
///
/// <https://tools.ietf.org/html/rfc427>
/// ```text
/// Name Value Definition
/// ---- ----- ----------
/// ORIGIN 1 See Section 5.1.1
/// AS_PATH 2 See Section 5.1.2
/// NEXT_HOP 3 See Section 5.1.3
/// MULTI_EXIT_DISC 4 See Section 5.1.4
/// LOCAL_PREF 5 See Section 5.1.5
/// ATOMIC_AGGREGATE 6 See Section 5.1.6
/// AGGREGATOR 7 See Section 5.1.7
/// ```
///
/// <https://tools.ietf.org/html/rfc4760>
/// Name Value
/// ---- -----
/// MP_REACH_NLRI 14
/// MP_UNREACH_NLRI 15
///
/// All attributes: <https://www.iana.org/assignments/bgp-parameters/bgp-parameters.xhtml#bgp-parameters-2>
/// All attributes currently defined and not Unassigned or Deprecated are included here.
/// To see the full list, check out IANA at:
/// <https://www.iana.org/assignments/bgp-parameters/bgp-parameters.xhtml#bgp-parameters-2>
#[allow(non_camel_case_types)]
#[derive(Debug, Primitive, PartialEq, Eq, Hash, Copy, Clone)]
pub enum AttrType {
Expand All @@ -79,13 +63,23 @@ pub enum AttrType {
CLUSTER_ID = 13,
MP_REACHABLE_NLRI = 14,
MP_UNREACHABLE_NLRI = 15,
/// <https://datatracker.ietf.org/doc/html/rfc4360>
EXTENDED_COMMUNITIES = 16,
AS4_PATH = 17,
AS4_AGGREGATOR = 18,
PMSI_TUNNEL = 22,
TUNNEL_ENCAPSULATION = 23,
TRAFFIC_ENGINEERING = 24,
IPV6_ADDRESS_SPECIFIC_EXTENDED_COMMUNITIES = 25,
AIGP = 26,
PE_DISTINGUISHER_LABELS = 27,
BGP_LS_ATTRIBUTE = 29,
LARGE_COMMUNITIES = 32,
// FIXME: 33 is BGPsec_Path
ATTRIBUTES_END = 33,
UNASSINGED = 39,
BGPSEC_PATH = 33,
SFP_ATTRIBUTE = 37,
BFD_DISCRIMINATOR = 38,
BGP_PREFIX_SID = 40,
ATTR_SET = 128,
}

#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -115,6 +109,8 @@ pub enum Attribute {
Aggregator(Asn, IpAddr),
Communities(Vec<Community>),
LargeCommunities(Vec<LargeCommunity>),
ExtendedCommunity(Vec<ExtendedCommunity>),
IPv6AddressSpecificExtendedCommunity(Vec<Ipv6AddressSpecificExtendedCommunity>),
OriginatorId(IpAddr),
Clusters(Vec<IpAddr>),
Nlri(Nlri),
Expand Down Expand Up @@ -243,33 +239,6 @@ impl AsPath {
}
}

/////////////////
// COMMUNITIES //
/////////////////

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Community {
NoExport,
NoAdvertise,
NoExportSubConfed,
Custom(Asn, u16),
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct LargeCommunity {
global_administrator: u32,
local_data: [u32; 2],
}

impl LargeCommunity {
pub fn new(global_administrator: u32, local_data: [u32; 2]) -> LargeCommunity {
LargeCommunity {
global_administrator,
local_data,
}
}
}

//////////
// NLRI //
//////////
Expand Down Expand Up @@ -352,27 +321,6 @@ impl Display for AtomicAggregate {
}
}

const NOEXPORT: &str ="no-export";

impl Display for Community {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
Community::NoExport => {
"no-export".to_string()
}
Community::NoAdvertise => {
"no-advertise".to_string()
}
Community::NoExportSubConfed => {
"no-export-sub-confed".to_string()
}
Community::Custom(asn, value) => {
format!("{}:{}", asn, value)
}
}
)
}
}

impl Display for NextHopAddress {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand Down Expand Up @@ -425,12 +373,6 @@ impl Serialize for Origin {
}
}

impl Serialize for Community {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
serializer.serialize_str(self.to_string().as_str())
}
}

impl Serialize for AtomicAggregate {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
serializer.serialize_str(self.to_string().as_str())
Expand Down
Loading

0 comments on commit 156eaf4

Please sign in to comment.