bgp_models/bgp/
mod.rs

1//! BGP messages and relevant structs.
2
3pub mod attributes;
4pub mod capabilities;
5pub mod community;
6pub mod elem;
7pub mod error;
8pub mod role;
9
10pub use crate::bgp::attributes::*;
11pub use crate::bgp::capabilities::*;
12pub use crate::bgp::community::*;
13pub use crate::bgp::elem::*;
14pub use crate::bgp::error::*;
15pub use crate::bgp::role::*;
16
17use crate::bgp::capabilities::BgpCapabilityType;
18use crate::bgp::error::BgpError;
19use crate::network::*;
20use serde::Serialize;
21use std::net::Ipv4Addr;
22
23#[derive(Debug, Primitive, Copy, Clone, Serialize, PartialEq)]
24pub enum BgpMessageType {
25    OPEN = 1,
26    UPDATE = 2,
27    NOTIFICATION = 3,
28    KEEPALIVE = 4,
29}
30
31// https://tools.ietf.org/html/rfc4271#section-4
32#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
33pub enum BgpMessage {
34    Open(BgpOpenMessage),
35    Update(BgpUpdateMessage),
36    Notification(BgpNotificationMessage),
37    KeepAlive(BgpKeepAliveMessage),
38}
39
40/// BGP Open Message
41///
42/// ```text
43///  0                   1                   2                   3
44///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
45///  +-+-+-+-+-+-+-+-+
46///  |    Version    |
47///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48///  |     My Autonomous System      |
49///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50///  |           Hold Time           |
51///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52///  |                         BGP Identifier                        |
53///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54///  | Opt Parm Len  |
55///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56///  |                                                               |
57///  |             Optional Parameters (variable)                    |
58///  |                                                               |
59///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60/// ```
61#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
62pub struct BgpOpenMessage {
63    pub version: u8,
64    pub asn: Asn,
65    pub hold_time: u16,
66    pub sender_ip: Ipv4Addr,
67    pub extended_length: bool,
68    pub opt_params: Vec<OptParam>,
69}
70
71#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
72pub struct OptParam {
73    pub param_type: u8,
74    pub param_len: u16,
75    pub param_value: ParamValue,
76}
77
78#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
79pub enum ParamValue {
80    Raw(Vec<u8>),
81    Capability(Capability),
82}
83
84/// BGP Capability.
85///
86/// - RFC3392: <https://datatracker.ietf.org/doc/html/rfc3392>
87/// - Capability codes: <https://www.iana.org/assignments/capability-codes/capability-codes.xhtml#capability-codes-2>
88#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
89pub struct Capability {
90    pub code: u8,
91    pub len: u8,
92    pub value: Vec<u8>,
93    pub capability_type: Option<BgpCapabilityType>,
94}
95
96#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
97pub struct BgpUpdateMessage {
98    pub withdrawn_prefixes: Vec<NetworkPrefix>,
99    pub attributes: Vec<Attribute>,
100    pub announced_prefixes: Vec<NetworkPrefix>,
101}
102
103#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
104pub struct BgpNotificationMessage {
105    pub error_code: u8,
106    pub error_subcode: u8,
107    pub error_type: Option<BgpError>,
108    pub data: Vec<u8>,
109}
110
111#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
112pub struct BgpKeepAliveMessage {}