bgpkit_parser/models/bgp/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! BGP messages and relevant structs.

pub mod attributes;
pub mod capabilities;
pub mod community;
pub mod elem;
pub mod error;
pub mod role;

pub use attributes::*;
pub use community::*;
pub use elem::*;
pub use error::*;
pub use role::*;

use crate::models::network::*;
use capabilities::BgpCapabilityType;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::net::Ipv4Addr;

pub type BgpIdentifier = Ipv4Addr;

#[derive(Debug, TryFromPrimitive, IntoPrimitive, Copy, Clone, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum BgpMessageType {
    OPEN = 1,
    UPDATE = 2,
    NOTIFICATION = 3,
    KEEPALIVE = 4,
}

// https://tools.ietf.org/html/rfc4271#section-4
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BgpMessage {
    Open(BgpOpenMessage),
    Update(BgpUpdateMessage),
    Notification(BgpNotificationMessage),
    KeepAlive,
}

impl BgpMessage {
    pub const fn msg_type(&self) -> BgpMessageType {
        match self {
            BgpMessage::Open(_) => BgpMessageType::OPEN,
            BgpMessage::Update(_) => BgpMessageType::UPDATE,
            BgpMessage::Notification(_) => BgpMessageType::NOTIFICATION,
            BgpMessage::KeepAlive => BgpMessageType::KEEPALIVE,
        }
    }
}

/// BGP Open Message
///
/// ```text
///  0                   1                   2                   3
///  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
///  +-+-+-+-+-+-+-+-+
///  |    Version    |
///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///  |     My Autonomous System      |
///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///  |           Hold Time           |
///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///  |                         BGP Identifier                        |
///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///  | Opt Parm Len  |
///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///  |                                                               |
///  |             Optional Parameters (variable)                    |
///  |                                                               |
///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BgpOpenMessage {
    pub version: u8,
    pub asn: Asn,
    pub hold_time: u16,
    pub sender_ip: Ipv4Addr,
    pub extended_length: bool,
    pub opt_params: Vec<OptParam>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OptParam {
    pub param_type: u8,
    pub param_len: u16,
    pub param_value: ParamValue,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ParamValue {
    Raw(Vec<u8>),
    Capability(Capability),
}

/// BGP Capability.
///
/// - RFC3392: <https://datatracker.ietf.org/doc/html/rfc3392>
/// - Capability codes: <https://www.iana.org/assignments/capability-codes/capability-codes.xhtml#capability-codes-2>
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Capability {
    pub ty: BgpCapabilityType,
    pub value: Vec<u8>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BgpUpdateMessage {
    pub withdrawn_prefixes: Vec<NetworkPrefix>,
    pub attributes: Attributes,
    pub announced_prefixes: Vec<NetworkPrefix>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BgpNotificationMessage {
    pub error: BgpError,
    pub data: Vec<u8>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_message_type() {
        let open = BgpMessage::Open(BgpOpenMessage {
            version: 4,
            asn: Asn::new_32bit(1),
            hold_time: 180,
            sender_ip: Ipv4Addr::new(192, 0, 2, 1),
            extended_length: false,
            opt_params: vec![],
        });
        assert_eq!(open.msg_type(), BgpMessageType::OPEN);

        let update = BgpMessage::Update(BgpUpdateMessage::default());
        assert_eq!(update.msg_type(), BgpMessageType::UPDATE);

        let notification = BgpMessage::Notification(BgpNotificationMessage {
            error: BgpError::Unknown(0, 0),
            data: vec![],
        });
        assert_eq!(notification.msg_type(), BgpMessageType::NOTIFICATION);

        let keepalive = BgpMessage::KeepAlive;
        assert_eq!(keepalive.msg_type(), BgpMessageType::KEEPALIVE);
    }

    #[test]
    #[cfg(feature = "serde")]
    fn test_serde() {
        let open = BgpMessage::Open(BgpOpenMessage {
            version: 4,
            asn: Asn::new_32bit(1),
            hold_time: 180,
            sender_ip: Ipv4Addr::new(192, 0, 2, 1),
            extended_length: false,
            opt_params: vec![],
        });
        let serialized = serde_json::to_string(&open).unwrap();
        let deserialized: BgpMessage = serde_json::from_str(&serialized).unwrap();
        assert_eq!(open, deserialized);

        let update = BgpMessage::Update(BgpUpdateMessage::default());
        let serialized = serde_json::to_string(&update).unwrap();
        let deserialized: BgpMessage = serde_json::from_str(&serialized).unwrap();
        assert_eq!(update, deserialized);

        let notification = BgpMessage::Notification(BgpNotificationMessage {
            error: BgpError::Unknown(0, 0),
            data: vec![],
        });
        let serialized = serde_json::to_string(&notification).unwrap();
        let deserialized: BgpMessage = serde_json::from_str(&serialized).unwrap();
        assert_eq!(notification, deserialized);

        let keepalive = BgpMessage::KeepAlive;
        let serialized = serde_json::to_string(&keepalive).unwrap();
        let deserialized: BgpMessage = serde_json::from_str(&serialized).unwrap();
        assert_eq!(keepalive, deserialized);
    }
}