asterust_junos/protocols/bgp/peer_options/
mod.rs1mod bfd_liveness_detection;
35mod graceful_restart;
36mod local_as;
37mod multihop;
38mod multipath;
39mod remove_private;
40
41pub use bfd_liveness_detection::BgpBfd;
42pub use graceful_restart::BgpGracefulRestart;
43pub use local_as::LocalAs;
44pub use multihop::Multihop;
45pub use multipath::BgpMultipath;
46pub use remove_private::RemovePrivate;
47
48use super::family::{parse_families, BgpFamily};
49use crate::{arg_of, has_leaf, statement_words, toggle};
50use asterust::config::ConfigLine;
51use bfd_liveness_detection::parse_bfd;
52use graceful_restart::parse_graceful_restart;
53use local_as::parse_local_as;
54use multihop::parse_multihop;
55use multipath::parse_multipath;
56use remove_private::parse_remove_private;
57
58#[derive(Clone, Debug, PartialEq, Eq)]
61#[non_exhaustive]
62pub struct BgpPeerOptions {
63 pub local_as_options: Option<LocalAs>,
64 pub multihop: Option<Multihop>,
65 pub remove_private: Option<RemovePrivate>,
66 pub bfd_liveness_detection: Option<BgpBfd>,
67 pub graceful_restart: Option<BgpGracefulRestart>,
68 pub multipath: Option<BgpMultipath>,
69 pub address_families: Vec<BgpFamily>,
71 pub authentication_algorithm: Option<String>,
73 pub has_authentication_key: bool,
75 pub authentication_key: Option<crate::Secret>,
77 pub authentication_key_chain: Option<String>,
78 pub hold_time: Option<String>,
80 pub out_delay: Option<String>,
82 pub tcp_mss: Option<String>,
83 pub keep: Option<String>,
85 pub local_preference: Option<String>,
86 pub preference: Option<String>,
87 pub bgp_identifier: Option<String>,
88 pub ipsec_sa: Option<String>,
89 pub local_interface: Option<String>,
91 pub metric_out: Option<String>,
93 pub advertise_peer_as: Option<bool>,
95 pub advertise_external: bool,
96 pub advertise_external_conditional: bool,
98 pub passive: bool,
99 pub mtu_discovery: bool,
100 pub advertise_inactive: bool,
101 pub as_override: bool,
102 pub damping: bool,
103 pub no_aggregator_id: bool,
104 pub no_client_reflect: bool,
105 pub log_updown: bool,
106 pub include_mp_next_hop: bool,
107 pub accept_remote_nexthop: bool,
108 pub vpn_apply_export: bool,
109}
110
111pub(super) fn parse_peer_options(line: &ConfigLine) -> BgpPeerOptions {
113 let field = |keyword: &str| arg_of(line, keyword).map(str::to_string);
114 let external = statement_words(line, "advertise-external");
115 BgpPeerOptions {
116 local_as_options: parse_local_as(line),
117 multihop: parse_multihop(line),
118 remove_private: parse_remove_private(line),
119 bfd_liveness_detection: parse_bfd(line),
120 graceful_restart: parse_graceful_restart(line),
121 multipath: parse_multipath(line),
122 address_families: parse_families(line),
123 authentication_algorithm: field("authentication-algorithm"),
124 has_authentication_key: arg_of(line, "authentication-key").is_some(),
125 authentication_key: crate::secret_value(arg_of(line, "authentication-key")),
126 authentication_key_chain: field("authentication-key-chain"),
127 hold_time: field("hold-time"),
128 out_delay: field("out-delay"),
129 tcp_mss: field("tcp-mss"),
130 keep: field("keep"),
131 local_preference: field("local-preference"),
132 preference: field("preference"),
133 bgp_identifier: field("bgp-identifier"),
134 ipsec_sa: field("ipsec-sa"),
135 local_interface: field("local-interface"),
136 metric_out: statement_words(line, "metric-out").map(|words| words.join(" ")),
137 advertise_peer_as: toggle(line, "advertise-peer-as"),
138 advertise_external: external.is_some(),
139 advertise_external_conditional: external.is_some_and(|words| words.contains(&"conditional")),
140 passive: has_leaf(line, "passive"),
141 mtu_discovery: has_leaf(line, "mtu-discovery"),
142 advertise_inactive: has_leaf(line, "advertise-inactive"),
143 as_override: has_leaf(line, "as-override"),
144 damping: has_leaf(line, "damping"),
145 no_aggregator_id: has_leaf(line, "no-aggregator-id"),
146 no_client_reflect: has_leaf(line, "no-client-reflect"),
147 log_updown: has_leaf(line, "log-updown"),
148 include_mp_next_hop: has_leaf(line, "include-mp-next-hop"),
149 accept_remote_nexthop: has_leaf(line, "accept-remote-nexthop"),
150 vpn_apply_export: has_leaf(line, "vpn-apply-export"),
151 }
152}
153
154#[cfg(test)]
155mod tests {
156 use crate::protocols::bgp::bgp;
157 use asterust::config::juniper;
158
159 const CONFIG: &str = r#"
161protocols {
162 bgp {
163 hold-time 30;
164 precision-timers;
165 group EBGP {
166 type external;
167 peer-as 65001;
168 local-as 65000 loops 2 private no-prepend-global-as;
169 multihop {
170 ttl 5;
171 no-nexthop-change;
172 }
173 authentication-algorithm hmac-sha-1-96;
174 authentication-key "$9$secret"; ## SECRET-DATA
175 hold-time 90;
176 out-delay 10;
177 tcp-mss 1400;
178 keep all;
179 local-preference 200;
180 preference 100;
181 metric-out minimum-igp 10;
182 remove-private all replace;
183 advertise-peer-as;
184 advertise-external conditional;
185 passive;
186 mtu-discovery;
187 as-override;
188 damping;
189 log-updown;
190 include-mp-next-hop;
191 neighbor 192.0.2.2 {
192 multihop ttl 3;
193 authentication-key-chain BGP-KEYS;
194 no-advertise-peer-as;
195 local-interface ge-0/0/0.0;
196 remove-private;
197 }
198 }
199 }
200}
201"#;
202
203 #[test]
204 fn extracts_group_session_options_without_the_key() {
205 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
206 let options = &bgp.groups[0].options;
207
208 let local_as = options.local_as_options.as_ref().unwrap();
209 assert_eq!(local_as.autonomous_system.as_deref(), Some("65000"));
210 assert_eq!(local_as.loops.as_deref(), Some("2"));
211 assert!(local_as.private);
212 assert!(!local_as.alias);
213 assert!(local_as.no_prepend_global_as);
214
215 let multihop = options.multihop.as_ref().unwrap();
216 assert_eq!(multihop.ttl.as_deref(), Some("5"));
217 assert!(multihop.no_nexthop_change);
218
219 assert_eq!(options.authentication_algorithm.as_deref(), Some("hmac-sha-1-96"));
220 assert!(options.has_authentication_key);
221 assert_eq!(
222 options.authentication_key.as_ref().map(crate::Secret::expose),
223 crate::secret::expected("$9$secret")
224 );
225 assert_eq!(options.hold_time.as_deref(), Some("90"));
226 assert_eq!(options.out_delay.as_deref(), Some("10"));
227 assert_eq!(options.tcp_mss.as_deref(), Some("1400"));
228 assert_eq!(options.keep.as_deref(), Some("all"));
229 assert_eq!(options.local_preference.as_deref(), Some("200"));
230 assert_eq!(options.preference.as_deref(), Some("100"));
231 assert_eq!(options.metric_out.as_deref(), Some("minimum-igp 10"));
232 assert_eq!(options.advertise_peer_as, Some(true));
233 assert!(options.advertise_external);
234 assert!(options.advertise_external_conditional);
235 assert!(options.passive);
236 assert!(options.mtu_discovery);
237 assert!(options.as_override);
238 assert!(options.damping);
239 assert!(options.log_updown);
240 assert!(options.include_mp_next_hop);
241 assert!(!options.no_aggregator_id);
242
243 let remove_private = options.remove_private.as_ref().unwrap();
244 assert!(remove_private.all);
245 assert!(remove_private.replace);
246 assert!(!remove_private.nearest);
247 }
248
249 #[test]
250 fn extracts_neighbor_options_and_older_multihop_form() {
251 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
252 let options = &bgp.groups[0].neighbors[0].options;
253 assert_eq!(options.multihop.as_ref().unwrap().ttl.as_deref(), Some("3"));
254 assert_eq!(options.authentication_key_chain.as_deref(), Some("BGP-KEYS"));
255 assert!(!options.has_authentication_key);
256 assert_eq!(options.advertise_peer_as, Some(false));
257 assert_eq!(options.local_interface.as_deref(), Some("ge-0/0/0.0"));
258 assert!(!options.remove_private.as_ref().unwrap().all);
259 assert_eq!(options.hold_time, None);
260 }
261
262 #[test]
263 fn extracts_global_options_and_flags() {
264 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
265 assert_eq!(bgp.options.hold_time.as_deref(), Some("30"));
266 assert!(bgp.precision_timers);
267 assert!(!bgp.disable);
268 }
269
270 #[test]
271 fn peer_options_default_to_unset() {
272 let bgp = bgp(&juniper::parse_braces(
273 "protocols {\n bgp {\n group G {\n type internal;\n }\n }\n}\n",
274 ))
275 .unwrap();
276 let options = &bgp.groups[0].options;
277 assert!(options.local_as_options.is_none());
278 assert!(options.multihop.is_none());
279 assert!(options.remove_private.is_none());
280 assert_eq!(options.advertise_peer_as, None);
281 assert!(!options.advertise_external);
282 assert!(!options.passive);
283 }
284}