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