Skip to main content

asterust_junos/protocols/bgp/
dynamic_neighbor.rs

1//! `dynamic-neighbor`, `allow` and `peer-as-list`
2//! `protocols { bgp { group <name> { dynamic-neighbor <name> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [dynamic-neighbor](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dynamic-neighbor-edit-protocols-bgp-group.html)
7//! - [allow](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/allow-edit-protocols-bgp.html)
8//! - [peer-as-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/peer-as-list-edit-protocols-bgp-group.html)
9
10use crate::{active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children};
11use asterust::config::ConfigLine;
12
13/// A `dynamic-neighbor <name>` block of a group, which accepts sessions from any address in its `allow` prefixes.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct DynamicNeighbor {
17    pub name: String,
18    /// Prefixes written `allow [ 10.0.0.0/8 ];`, or `all`.
19    pub allow: Vec<String>,
20    pub peer_limit: Option<String>,
21    pub authentication_algorithm: Option<String>,
22    /// The name of the key chain.
23    pub authentication_key_chain: Option<String>,
24    /// An `authentication-key` is configured.
25    pub has_authentication_key: bool,
26    /// The `authentication-key`; `None` unless the `secrets` feature is enabled.
27    pub authentication_key: Option<crate::Secret>,
28    pub peer_auto_discovery: Option<PeerAutoDiscovery>,
29}
30
31/// The `peer-auto-discovery` block of a `dynamic-neighbor`, used for BGP unnumbered.
32#[derive(Clone, Debug, Default, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct PeerAutoDiscovery {
35    /// `family inet6 { ipv6-nd; }`.
36    pub ipv6_nd: bool,
37    /// Interfaces written `interface <name>;` or `interface <name> { enable; }`.
38    pub interfaces: Vec<String>,
39    /// Interfaces written `interface <name> { disable; }`.
40    pub disabled_interfaces: Vec<String>,
41}
42
43/// Every `allow` entry of `line`, whether written as a list or as separate statements.
44pub(super) fn parse_allow(line: &ConfigLine) -> Vec<String> {
45    args_of(line, "allow").into_iter().flat_map(list_values).collect()
46}
47
48pub(super) fn parse_dynamic_neighbor(name: &str, block: &ConfigLine) -> DynamicNeighbor {
49    DynamicNeighbor {
50        name: name.to_string(),
51        allow: parse_allow(block),
52        peer_limit: arg_of(block, "peer-limit").map(str::to_string),
53        authentication_algorithm: arg_of(block, "authentication-algorithm").map(str::to_string),
54        authentication_key_chain: arg_of(block, "authentication-key-chain").map(str::to_string),
55        has_authentication_key: arg_of(block, "authentication-key").is_some(),
56        authentication_key: crate::secret_value(arg_of(block, "authentication-key")),
57        peer_auto_discovery: child_block(block, "peer-auto-discovery").map(parse_peer_auto_discovery),
58    }
59}
60
61fn parse_peer_auto_discovery(discovery: &ConfigLine) -> PeerAutoDiscovery {
62    let (disabled, enabled): (Vec<_>, Vec<_>) = named_children(discovery, "interface")
63        .map(|(name, interface)| (name.to_string(), has_leaf(interface, "disable")))
64        .partition(|(_, disabled)| *disabled);
65    PeerAutoDiscovery {
66        ipv6_nd: mentions(discovery, "ipv6-nd"),
67        interfaces: enabled.into_iter().map(|(name, _)| name).collect(),
68        disabled_interfaces: disabled.into_iter().map(|(name, _)| name).collect(),
69    }
70}
71
72/// Whether `text` appears as a statement anywhere under `line`.
73fn mentions(line: &ConfigLine, text: &str) -> bool {
74    active_children(line).any(|child| child.text == text || mentions(child, text))
75}
76
77#[cfg(test)]
78mod tests {
79    use crate::protocols::bgp::bgp;
80    use asterust::config::juniper;
81
82    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
83    const CONFIG: &str = r#"
84protocols {
85    bgp {
86        group CUSTOMERS {
87            type external;
88            allow [ 192.0.2.0/24 198.51.100.0/24 ];
89            allow 203.0.113.0/24;
90            peer-as-list CUSTOMER-ASNS;
91            dynamic-neighbor DYN {
92                allow [ 10.0.0.0/8 ];
93                peer-limit 100;
94                authentication-algorithm md5;
95                authentication-key "$9$secret";
96            }
97        }
98    }
99}
100"#;
101
102    #[test]
103    fn reads_allow_peer_as_list_and_dynamic_neighbors() {
104        let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
105        let group = &bgp.groups[0];
106        assert_eq!(group.allow, ["192.0.2.0/24", "198.51.100.0/24", "203.0.113.0/24"]);
107        assert_eq!(group.peer_as_list.as_deref(), Some("CUSTOMER-ASNS"));
108
109        let dynamic = &group.dynamic_neighbors[0];
110        assert_eq!(dynamic.name, "DYN");
111        assert_eq!(dynamic.allow, ["10.0.0.0/8"]);
112        assert_eq!(dynamic.peer_limit.as_deref(), Some("100"));
113        assert_eq!(dynamic.authentication_algorithm.as_deref(), Some("md5"));
114        assert!(dynamic.has_authentication_key);
115        assert_eq!(
116            dynamic.authentication_key.as_ref().map(crate::Secret::expose),
117            crate::secret::expected("$9$secret")
118        );
119        assert_eq!(dynamic.authentication_key_chain, None);
120        assert_eq!(dynamic.peer_auto_discovery, None);
121    }
122
123    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
124    const UNNUMBERED: &str = r#"
125protocols {
126    bgp {
127        group underlay {
128            dynamic-neighbor bgp_unnumbered {
129                peer-auto-discovery {
130                    family inet6 {
131                        ipv6-nd;
132                    }
133                    interface xe-0/0/0.0;
134                    interface xe-0/0/1.0 {
135                        enable;
136                    }
137                    interface xe-0/0/2.0 {
138                        disable;
139                    }
140                }
141            }
142            dynamic-neighbor documented {
143                peer-auto-discovery {
144                    family {
145                        inet6 {
146                            ipv6-nd;
147                        }
148                    }
149                }
150            }
151        }
152    }
153}
154"#;
155
156    #[test]
157    fn reads_peer_auto_discovery_for_bgp_unnumbered() {
158        let bgp = bgp(&juniper::parse_braces(UNNUMBERED)).unwrap();
159        let discovery = bgp.groups[0].dynamic_neighbors[0].peer_auto_discovery.as_ref().unwrap();
160        assert!(discovery.ipv6_nd);
161        assert_eq!(discovery.interfaces, ["xe-0/0/0.0", "xe-0/0/1.0"]);
162        assert_eq!(discovery.disabled_interfaces, ["xe-0/0/2.0"]);
163    }
164
165    #[test]
166    fn reads_the_nested_family_form() {
167        let bgp = bgp(&juniper::parse_braces(UNNUMBERED)).unwrap();
168        let discovery = bgp.groups[0].dynamic_neighbors[1].peer_auto_discovery.as_ref().unwrap();
169        assert!(discovery.ipv6_nd);
170        assert!(discovery.interfaces.is_empty());
171    }
172}