JFLib

netpkt/bgp.rs#

Source: lib/netpkt/bgp.rs

bgp -- BGP-4 messages byte for byte (RFC 4271): the 19-byte header with its marker, OPEN with
its optional parameters and the capabilities of RFC 5492 (multiprotocol IPv4 unicast of RFC
4760, route refresh of RFC 2918, 4-octet AS numbers of RFC 6793), UPDATE with withdrawn routes,
path attributes and NLRI, NOTIFICATION with the codes and subcodes of RFC 4271 and the Cease
subcodes of RFC 4486, KEEPALIVE and ROUTE-REFRESH; the checks of RFC 4271 6.1 to 6.3 as the
NOTIFICATION a receiver sends

  frame(stream)            the length of the first message of a TCP byte stream once it is
                           complete (Ok(None): more bytes needed) after the header checks of
                           6.1 (marker, length, type and the length of the type)
  BgpView::parse(b)        one message: kind(), len(), body(), marker_ok(); None when the
                           bytes are shorter than the header or than its length field
  Message::read(b, as4)    a whole message as a record (Err: the BgpErr whose NOTIFICATION a
                           receiver sends); to_bytes(as4) writes it with marker and length.
                           `as4`: the session negotiated 4-octet AS numbers (AS_PATH and
                           AGGREGATOR carry them)
  Open { version, asn, hold, id, params }   `asn` as on the wire (AS_TRANS for a 4-octet AS);
                           Open::new(asn, hold, id, caps) fills it in; caps(), as4(),
                           peer_as(), has_cap(code)
  OptParam::Caps(Vec<Cap>) / Other;  Cap::Mp { afi, safi }, RouteRefresh, As4(asn), Other
  Update { withdrawn, attrs, nlri }; Attr { flags, val } with Attr::new(val) (the flags the
                           RFCs give the type) and kind(); AttrVal::Origin, AsPath (segments),
                           NextHop, Med, LocalPref, AtomicAggregate, Aggregator, Communities,
                           As4Path, As4Aggregator, Other (unknown types byte for byte);
                           Update::get(kind), origin(), as_path(), next_hop(), med(),
                           local_pref(), communities()
  Segment { set, asns }    an AS_SET or AS_SEQUENCE; path_len(segs) counts a set as one AS
                           (9.1.2.2), path_text(segs) "65002 65003 {65010,65011}"
  Notification { code, sub, data };  error_name(code, sub), code_name(code)
  BgpErr { code, sub, data }   what a receiver refuses and the NOTIFICATION it sends for it;
                           text() in English
  prefix_len(p) / write_prefix / read_prefixes   NLRI: the length in bits, then the bytes needed
  as_path_4(value)         a dissector's guess whether an AS_PATH carries 4-octet numbers
  origin_code(o) / origin_name(o) / community_text(c) / afi_name / cap_name   for dissectors

Constants: PORT (179), VERSION, HDR_LEN, MAX_LEN, MARKER, the minimum lengths OPEN_MIN,
UPDATE_MIN, NOTIFICATION_MIN, ROUTE_REFRESH_LEN, the types T_*, OPT_CAPS, the capability codes
CAP_*, AFI_IPV4 / SAFI_UNICAST, AS_TRANS, the attribute flags F_* and types A_*, ORIGIN_*,
SEG_*, the error codes E_* with their subcodes (HE_*, OE_*, UE_*, CE_*).

The codec reads and writes; what needs the session (the peer's AS, its BGP identifier against
the own one, the hold time a speaker accepts, NEXT_HOP semantics, AS path loops, LOCAL_PREF
from an internal peer) is the speaker's business.  Attributes keep their flags and order, so a
message read and written again is the same bytes (the unused low four flag bits too); a
malformed AS4_PATH or AS4_AGGREGATOR is kept as Other (RFC 6793 6: attribute discard).  RFC
7606 (treat-as-withdraw) is not here: every error is a NOTIFICATION as RFC 4271 says.
/// the TCP port of BGP
PORT: u16 = 179
/// BGP version 4
VERSION: u8 = 4
/// the header (marker, length, type) and the longest message (RFC 4271 4)
HDR_LEN: usize = 19
MAX_LEN: usize = 4096
/// sixteen bytes of ones
MARKER: [u8; 16] = [0xFF; 16]
/// the shortest OPEN, UPDATE and NOTIFICATION, the length of ROUTE-REFRESH
OPEN_MIN: usize = 29
UPDATE_MIN: usize = 23
NOTIFICATION_MIN: usize = 21
ROUTE_REFRESH_LEN: usize = 23
/// message types
T_OPEN: u8 = 1
T_UPDATE: u8 = 2
T_NOTIFICATION: u8 = 3
T_KEEPALIVE: u8 = 4
T_ROUTE_REFRESH: u8 = 5
/// the optional parameter that carries capabilities (RFC 5492)
OPT_CAPS: u8 = 2
/// capability codes: multiprotocol (RFC 4760), route refresh (RFC 2918), 4-octet AS (RFC 6793)
CAP_MP: u8 = 1
CAP_ROUTE_REFRESH: u8 = 2
CAP_AS4: u8 = 65
/// the address family and sub-family of IPv4 unicast
AFI_IPV4: u16 = 1
SAFI_UNICAST: u8 = 1
/// the 2-octet AS a 4-octet one shows as to an old speaker (RFC 6793)
AS_TRANS: u16 = 23456
/// attribute flags
F_OPTIONAL: u8 = 0x80
F_TRANSITIVE: u8 = 0x40
F_PARTIAL: u8 = 0x20
F_EXTENDED: u8 = 0x10
/// attribute types
A_ORIGIN: u8 = 1
A_AS_PATH: u8 = 2
A_NEXT_HOP: u8 = 3
A_MED: u8 = 4
A_LOCAL_PREF: u8 = 5
A_ATOMIC_AGGREGATE: u8 = 6
A_AGGREGATOR: u8 = 7
A_COMMUNITIES: u8 = 8
A_AS4_PATH: u8 = 17
A_AS4_AGGREGATOR: u8 = 18
/// ORIGIN values
ORIGIN_IGP: u8 = 0
ORIGIN_EGP: u8 = 1
ORIGIN_INCOMPLETE: u8 = 2
/// AS_PATH segment types
SEG_SET: u8 = 1
SEG_SEQUENCE: u8 = 2
/// error codes (RFC 4271 4.5)
E_HEADER: u8 = 1
E_OPEN: u8 = 2
E_UPDATE: u8 = 3
E_HOLD: u8 = 4
E_FSM: u8 = 5
E_CEASE: u8 = 6
/// Message Header Error subcodes
HE_SYNC: u8 = 1
HE_LENGTH: u8 = 2
HE_TYPE: u8 = 3
/// OPEN Message Error subcodes (7: RFC 5492)
OE_UNSPECIFIC: u8 = 0
OE_VERSION: u8 = 1
OE_PEER_AS: u8 = 2
OE_ID: u8 = 3
OE_OPT_PARAM: u8 = 4
OE_HOLD: u8 = 6
OE_CAPABILITY: u8 = 7
/// UPDATE Message Error subcodes
UE_ATTR_LIST: u8 = 1
UE_UNRECOGNIZED: u8 = 2
UE_MISSING: u8 = 3
UE_FLAGS: u8 = 4
UE_LENGTH: u8 = 5
UE_ORIGIN: u8 = 6
UE_NEXT_HOP: u8 = 8
UE_OPTIONAL: u8 = 9
UE_NETWORK: u8 = 10
UE_AS_PATH: u8 = 11
/// Cease subcodes (RFC 4486)
CE_MAX_PREFIXES: u8 = 1
CE_SHUTDOWN: u8 = 2
CE_DECONFIGURED: u8 = 3
CE_RESET: u8 = 4
CE_REJECTED: u8 = 5
CE_CONFIG: u8 = 6
CE_COLLISION: u8 = 7
CE_RESOURCES: u8 = 8
  • struct BgpErr what a receiver refuses: the code, subcode and data of the NOTIFICATION it sends
    • code: u8
    • sub: u8
    • data: Vec<u8>

impl BgpErr

  • fn new(code: u8, sub: u8, data: Vec<u8>) -> BgpErr

  • fn notification(&self) -> Notification the NOTIFICATION for it

  • fn text(&self) -> String "UPDATE Message Error / Missing Well-known Attribute"

  • fn code_name(code: u8) -> &'static str the name of an error code (RFC 4271 4.5)

  • fn error_name(code: u8, sub: u8) -> &'static str the name of a subcode ("Bad Peer AS"); the code's name where it has none

  • fn frame(b: &[u8]) -> Result<Option<usize>, BgpErr> the length of the first message in a TCP byte stream once all of it is there (None: more bytes needed); Err with the NOTIFICATION of 6.1 for a bad header

  • struct BgpView<'a> one message: the header and as many bytes as its length says

impl<'a> BgpView<'a>

  • fn parse(b: &'a [u8]) -> Option<BgpView<'a>> None when the bytes are shorter than the header or than its length field (bytes after the length are left out)

  • fn marker_ok(&self) -> bool

  • fn len(&self) -> usize

  • fn is_empty(&self) -> bool

  • fn kind(&self) -> u8

  • fn body(&self) -> &'a [u8]

  • fn bytes(&self) -> &'a [u8]

  • fn type_name(t: u8) -> &'static str the name of a message type ("UPDATE")

  • fn prefix_len(p: &Prefix4) -> usize the bytes a prefix takes in NLRI: its length, then the bytes that hold its bits

  • fn write_prefix(out: &mut Vec<u8>, p: &Prefix4) appends a prefix as NLRI

  • fn read_prefixes(b: &[u8]) -> Result<Vec<Prefix4>, BgpErr> the prefixes of a withdrawn-routes or NLRI field; Err(Invalid Network Field) for a length above 32 or bytes that end early. Bits after the length are dropped (the network form).

  • enum Cap a capability (RFC 5492)

    • Mp { afi: u16, safi: u8 } -- multiprotocol extensions (RFC 4760)
    • RouteRefresh -- route refresh (RFC 2918)
    • As4(u32) -- 4-octet AS numbers with the speaker's AS (RFC 6793)
    • Other { code: u8, data: Vec<u8> } -- anything else, byte for byte

impl Cap

  • fn code(&self) -> u8

  • fn cap_name(code: u8) -> &'static str the name of a capability code

  • fn afi_name(afi: u16, safi: u8) -> String the name of an address family ("IPv4")

  • enum OptParam an optional parameter of an OPEN

    • Caps(Vec<Cap>) -- capabilities (parameter type 2)
    • Other { kind: u8, data: Vec<u8> } -- another type, byte for byte (a receiver refuses it: Unsupported Optional Parameter)
  • struct Open an OPEN message (RFC 4271 4.2)

    • version: u8
    • asn: u16 -- My Autonomous System as on the wire (AS_TRANS for a 4-octet AS)
    • hold: u16
    • id: Ipv4
    • params: Vec<OptParam>

impl Open

  • fn new(asn: u32, hold: u16, id: Ipv4, caps: Vec<Cap>) -> Open version 4 from asn (AS_TRANS on the wire above 65535) with one parameter of capabilities (none when caps is empty)

  • fn caps(&self) -> impl Iterator<Item = &Cap> every capability, in order

  • fn has_cap(&self, code: u8) -> bool

  • fn as4(&self) -> Option<u32> the AS of the 4-octet capability

  • fn peer_as(&self) -> u32 the speaker's AS: the 4-octet one where it sent the capability, else My Autonomous System

  • fn ipv4_unicast(&self) -> bool it offers IPv4 unicast (multiprotocol), or names no family at all (then IPv4 unicast alone, RFC 4760 8)

  • fn read(b: &[u8]) -> Result<Open, BgpErr> the body of an OPEN (6.2): the version, the optional parameters as their lengths say (else OPEN Message Error / Unspecific), known parameter types only, a hold time of 0 or at least 3 s, a BGP identifier that is a unicast host address

  • struct Segment an AS_SET or an AS_SEQUENCE

    • set: bool
    • asns: Vec<u32>

impl Segment

  • fn seq(asns: Vec<u32>) -> Segment

  • fn path_len(segs: &[Segment]) -> usize the length of an AS path for the decision process: an AS_SET counts as one AS (9.1.2.2)

  • fn path_text(segs: &[Segment]) -> String "65002 65003 {65010,65011}"; "" for an empty path

  • fn origin_code(o: u8) -> &'static str "i", "e", "?"

  • fn origin_name(o: u8) -> &'static str "IGP", "EGP", "INCOMPLETE"

  • fn community_text(c: u32) -> String a community as people write it: "65001:100", or the well-known names of RFC 1997

  • fn attr_name(t: u8) -> &'static str the name of an attribute type ("AS_PATH")

  • enum AttrVal the value of a path attribute

    • Origin(u8)
    • AsPath(Vec<Segment>)
    • NextHop(Ipv4)
    • Med(u32)
    • LocalPref(u32)
    • AtomicAggregate
    • Aggregator { asn: u32, id: Ipv4 } -- the AS and BGP identifier of the speaker that aggregated
    • Communities(Vec<u32>)
    • As4Path(Vec<Segment>) -- the 4-octet path next to an AS_PATH of 2-octet numbers (RFC 6793)
    • As4Aggregator { asn: u32, id: Ipv4 }
    • Other { kind: u8, data: Vec<u8> } -- a type this codec does not know (or a malformed AS4 attribute), byte for byte
  • struct Attr a path attribute with its flags (the extended length bit is set when the value needs it)

    • flags: u8
    • val: AttrVal
  • fn as_path_4(v: &[u8]) -> bool a dissector's guess: the AS_PATH value reads as 4-octet numbers (a session between speakers with the capability) and not as 2-octet ones, or as both and the 2-octet reading gives AS 0 (the high halves of 4-octet numbers)

impl Attr

  • fn new(val: AttrVal) -> Attr a known attribute with the flags its type has

  • fn kind(&self) -> u8

  • fn value(&self, as4: bool) -> Vec<u8> the value bytes (as4: AS_PATH and AGGREGATOR with 4-octet numbers)

  • fn write(&self, out: &mut Vec<u8>, as4: bool) appends flags, type, length (two bytes with the extended length bit) and value

  • struct Update an UPDATE message (RFC 4271 4.3)

    • withdrawn: Vec<Prefix4>
    • attrs: Vec<Attr>
    • nlri: Vec<Prefix4>

impl Update

  • fn get(&self, kind: u8) -> Option<&AttrVal> the attribute of a type

  • fn origin(&self) -> Option<u8>

  • fn as_path(&self) -> Option<&[Segment]>

  • fn as4_path(&self) -> Option<&[Segment]>

  • fn next_hop(&self) -> Option<Ipv4>

  • fn med(&self) -> Option<u32>

  • fn local_pref(&self) -> Option<u32>

  • fn communities(&self) -> Option<&[u32]>

  • fn wire_len(&self, as4: bool) -> usize the bytes of the message this update makes (to keep it within MAX_LEN)

  • fn read(b: &[u8], as4: bool) -> Result<Update, BgpErr> the body of an UPDATE (6.3): the lengths fit the message (else Malformed Attribute List), the prefixes are valid (Invalid Network Field), the attributes as read_attrs says, and NLRI come with ORIGIN, AS_PATH and NEXT_HOP (Missing Well-known Attribute, its type as data)

  • struct Notification a NOTIFICATION message (RFC 4271 4.5)

    • code: u8
    • sub: u8
    • data: Vec<u8>

impl Notification

  • fn new(code: u8, sub: u8) -> Notification

  • fn text(&self) -> String "Cease / Administrative Reset"

  • enum Message a BGP message

    • Open(Open)
    • Update(Update)
    • Notification(Notification)
    • Keepalive
    • RouteRefresh { afi: u16, safi: u8 } -- a request to send the routes of a family again (RFC 2918)

impl Message

  • fn kind(&self) -> u8
  • fn read(b: &[u8], as4: bool) -> Result<Message, BgpErr> one whole message (the first of b; frame() says how long it is) with the checks of 6.1 to 6.3
  • fn to_bytes(&self, as4: bool) -> Vec<u8> the message on the wire: marker, length, type, body

Source: docs/API.md in the JFLib repository.