Skip to main content

asterust_junos/protocols/bgp/peer_options/
mod.rs

1//! `local-as`, `hold-time`, `authentication-algorithm` and the other peer options
2//! `protocols { bgp { [group <name> { [neighbor <address> {] ... } } } }`; valid at the `bgp`, `group` and `neighbor` levels.
3//!
4//! # See also
5//!
6//! - [authentication-algorithm](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/authentication-algorithm-edit-protocols-bgp.html)
7//! - [authentication-key (Protocols BGP and BMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/authentication-key-edit-protocols-bgp.html)
8//! - [authentication-key-chain (Protocols BGP and BMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/authentication-key-chain-edit-protocols-bgp.html)
9//! - [hold-time (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/hold-time-edit-protocols-bgp.html)
10//! - [out-delay](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/out-delay-edit-protocols-bgp.html)
11//! - [metric-out](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/metric-out-edit-protocols-bgp.html)
12//! - [local-preference](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/local-preference-edit-protocols-bgp.html)
13//! - [preference (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/preference-edit-protocols-bgp.html)
14//! - [keep](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/keep-edit-protocols-bgp.html)
15//! - [tcp-mss (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tcp-mss-edit-protocols-bgp.html)
16//! - [bgp-identifier](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bgp-identifier-edit-protocols-bgp.html)
17//! - [ipsec-sa (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ipsec-sa-edit-protocols-bgp.html)
18//! - [local-interface (IPv6)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/local-interface-edit-protocols-bgp.html)
19//! - [advertise-peer-as](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/advertise-peer-as-edit-protocols-bgp.html)
20//! - [no advertise-peer-as](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-advertise-peer-as-edit-protocols-bgp.html)
21//! - [advertise-inactive](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/advertise-inactive-edit-protocols-bgp.html)
22//! - [advertise-external](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/advertise-external-edit-protocols-bgp.html)
23//! - [as-override](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/as-override-edit-protocols-bgp.html)
24//! - [damping (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/damping-edit-protocols-bgp.html)
25//! - [passive (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/passive-edit-protocols-bgp.html)
26//! - [mtu-discovery](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mtu-discovery-edit-protocols-bgp.html)
27//! - [log-updown](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/log-updown-edit-protocols-bgp.html)
28//! - [no-aggregator-id](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-aggregator-id-edit-protocols-bgp.html)
29//! - [no-client-reflect](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-client-reflect-edit-protocols-bgp.html)
30//! - [include-mp-next-hop](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/include-mp-next-hop-edit-protocols-bgp.html)
31//! - [accept-remote-nexthop](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accept-remote-nexthop-edit-protocols-bgp.html)
32//! - [vpn-apply-export](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vpn-apply-export-edit-protocols-bgp-vp.html)
33
34mod 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/// The session settings shared by the `bgp`, `group` and `neighbor` levels. Settings inherited from an outer level
59/// are not resolved. The `authentication-key` is read only with the `secrets` feature.
60#[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    /// The `family` statements of this level, which replace (not extend) those of an outer level.
70    pub address_families: Vec<BgpFamily>,
71    /// `aes-128-cmac-96`, `hmac-sha-1-96` or `md5`.
72    pub authentication_algorithm: Option<String>,
73    /// An `authentication-key` is configured.
74    pub has_authentication_key: bool,
75    /// The `authentication-key`; `None` unless the `secrets` feature is enabled.
76    pub authentication_key: Option<crate::Secret>,
77    pub authentication_key_chain: Option<String>,
78    /// Seconds.
79    pub hold_time: Option<String>,
80    /// Seconds.
81    pub out_delay: Option<String>,
82    pub tcp_mss: Option<String>,
83    /// `all` or `none`.
84    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    /// The interface of an IPv6 link-local neighbor address.
90    pub local_interface: Option<String>,
91    /// The `metric-out` words as written, e.g. `"100"`, `"minimum-igp 10"` or `"igp delay-med-update"`.
92    pub metric_out: Option<String>,
93    /// `Some(true)` for `advertise-peer-as`, `Some(false)` for `no-advertise-peer-as`.
94    pub advertise_peer_as: Option<bool>,
95    pub advertise_external: bool,
96    /// `advertise-external conditional`.
97    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
111/// Reads the peer options of a `bgp`, `group` or `neighbor` block.
112pub(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    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
160    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}