Skip to main content

asterust_junos/system/
internet_options.rs

1//! `system { internet-options { ... } }`.
2
3use crate::{arg_of, has_leaf, statement_words, value_after};
4use asterust::config::ConfigLine;
5
6/// An `icmpv4-rate-limit` or `icmpv6-rate-limit`.
7#[derive(Clone, Debug, Default, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct IcmpRateLimit {
10    /// Seconds.
11    pub bucket_size: Option<String>,
12    /// Packets per second.
13    pub packet_rate: Option<String>,
14}
15
16/// The `system internet-options` configuration.
17///
18/// Options that come as an enable/disable pair (`path-mtu-discovery` and `no-path-mtu-discovery`) are `None` when
19/// neither is written, `Some(true)` for the plain form and `Some(false)` for the `no-` form.
20#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct InternetOptions {
23    pub path_mtu_discovery: Option<bool>,
24    pub ipv6_path_mtu_discovery: Option<bool>,
25    pub gre_path_mtu_discovery: Option<bool>,
26    pub ipip_path_mtu_discovery: Option<bool>,
27    pub ipv6_reject_zero_hop_limit: Option<bool>,
28    pub source_quench: Option<bool>,
29    pub icmpv4_rate_limit: Option<IcmpRateLimit>,
30    pub icmpv6_rate_limit: Option<IcmpRateLimit>,
31    pub tcp_drop_synfin_set: bool,
32    pub no_tcp_rfc1323: bool,
33    pub no_tcp_rfc1323_paws: bool,
34    /// `drop-all-tcp` or `drop-tcp-with-syn-only`.
35    pub no_tcp_reset: Option<String>,
36    pub tcp_mss: Option<String>,
37    pub source_port_upper_limit: Option<String>,
38    pub ipv6_duplicate_addr_detection_transmits: Option<String>,
39}
40
41pub(super) fn parse_internet_options(options: &ConfigLine) -> InternetOptions {
42    let field = |keyword: &str| arg_of(options, keyword).map(str::to_string);
43    InternetOptions {
44        path_mtu_discovery: toggle(options, "path-mtu-discovery"),
45        ipv6_path_mtu_discovery: toggle(options, "ipv6-path-mtu-discovery"),
46        gre_path_mtu_discovery: toggle(options, "gre-path-mtu-discovery"),
47        ipip_path_mtu_discovery: toggle(options, "ipip-path-mtu-discovery"),
48        ipv6_reject_zero_hop_limit: toggle(options, "ipv6-reject-zero-hop-limit"),
49        source_quench: toggle(options, "source-quench"),
50        icmpv4_rate_limit: rate_limit(options, "icmpv4-rate-limit"),
51        icmpv6_rate_limit: rate_limit(options, "icmpv6-rate-limit"),
52        tcp_drop_synfin_set: has_leaf(options, "tcp-drop-synfin-set"),
53        no_tcp_rfc1323: has_leaf(options, "no-tcp-rfc1323"),
54        no_tcp_rfc1323_paws: has_leaf(options, "no-tcp-rfc1323-paws"),
55        no_tcp_reset: field("no-tcp-reset"),
56        tcp_mss: field("tcp-mss"),
57        source_port_upper_limit: statement_words(options, "source-port")
58            .and_then(|words| value_after(&words, "upper-limit")),
59        ipv6_duplicate_addr_detection_transmits: field("ipv6-duplicate-addr-detection-transmits"),
60    }
61}
62
63/// `Some(true)` for `<name>;`, `Some(false)` for `no-<name>;`, `None` if neither is present.
64fn toggle(options: &ConfigLine, name: &str) -> Option<bool> {
65    if has_leaf(options, name) {
66        Some(true)
67    } else if has_leaf(options, &format!("no-{name}")) {
68        Some(false)
69    } else {
70        None
71    }
72}
73
74/// `<keyword> bucket-size <n> packet-rate <n>`, on one line or as one child statement per option.
75fn rate_limit(options: &ConfigLine, keyword: &str) -> Option<IcmpRateLimit> {
76    let words = statement_words(options, keyword)?;
77    Some(IcmpRateLimit {
78        bucket_size: value_after(&words, "bucket-size"),
79        packet_rate: value_after(&words, "packet-rate"),
80    })
81}
82
83#[cfg(test)]
84mod tests {
85    use crate::system::system;
86    use asterust::config::juniper;
87
88    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
89    const INTERNET_OPTIONS: &str = r#"
90system {
91    internet-options {
92        icmpv4-rate-limit bucket-size 10 packet-rate 500;
93        icmpv6-rate-limit {
94            bucket-size 3;
95            packet-rate 200;
96        }
97        no-ipv6-reject-zero-hop-limit;
98        no-source-quench;
99        path-mtu-discovery;
100        no-ipv6-path-mtu-discovery;
101        no-tcp-reset drop-all-tcp;
102        no-tcp-rfc1323;
103        no-tcp-rfc1323-paws;
104        tcp-drop-synfin-set;
105        tcp-mss 1400;
106        ipv6-duplicate-addr-detection-transmits 5;
107        source-port {
108            upper-limit 40000;
109        }
110    }
111}
112"#;
113
114    #[test]
115    fn extracts_toggle_options() {
116        let options = system(&juniper::parse_braces(INTERNET_OPTIONS))
117            .unwrap()
118            .internet_options
119            .unwrap();
120        assert_eq!(options.path_mtu_discovery, Some(true));
121        assert_eq!(options.ipv6_path_mtu_discovery, Some(false));
122        assert_eq!(options.ipv6_reject_zero_hop_limit, Some(false));
123        assert_eq!(options.source_quench, Some(false));
124        assert_eq!(options.gre_path_mtu_discovery, None);
125        assert_eq!(options.ipip_path_mtu_discovery, None);
126    }
127
128    #[test]
129    fn extracts_icmp_rate_limits_in_line_and_block_form() {
130        let options = system(&juniper::parse_braces(INTERNET_OPTIONS))
131            .unwrap()
132            .internet_options
133            .unwrap();
134        let v4 = options.icmpv4_rate_limit.unwrap();
135        assert_eq!(v4.bucket_size.as_deref(), Some("10"));
136        assert_eq!(v4.packet_rate.as_deref(), Some("500"));
137        let v6 = options.icmpv6_rate_limit.unwrap();
138        assert_eq!(v6.bucket_size.as_deref(), Some("3"));
139        assert_eq!(v6.packet_rate.as_deref(), Some("200"));
140    }
141
142    #[test]
143    fn extracts_tcp_and_port_options() {
144        let options = system(&juniper::parse_braces(INTERNET_OPTIONS))
145            .unwrap()
146            .internet_options
147            .unwrap();
148        assert!(options.tcp_drop_synfin_set);
149        assert!(options.no_tcp_rfc1323);
150        assert!(options.no_tcp_rfc1323_paws);
151        assert_eq!(options.no_tcp_reset.as_deref(), Some("drop-all-tcp"));
152        assert_eq!(options.tcp_mss.as_deref(), Some("1400"));
153        assert_eq!(options.source_port_upper_limit.as_deref(), Some("40000"));
154        assert_eq!(options.ipv6_duplicate_addr_detection_transmits.as_deref(), Some("5"));
155    }
156
157    #[test]
158    fn unset_options_are_none_or_false() {
159        let options = system(&juniper::parse_braces(
160            "system {\n    internet-options {\n        tcp-mss 1300;\n    }\n}\n",
161        ))
162        .unwrap()
163        .internet_options
164        .unwrap();
165        assert_eq!(options.path_mtu_discovery, None);
166        assert_eq!(options.icmpv4_rate_limit, None);
167        assert!(!options.tcp_drop_synfin_set);
168        assert_eq!(options.source_port_upper_limit, None);
169    }
170
171    #[test]
172    fn no_internet_options_is_none() {
173        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
174        assert!(system.internet_options.is_none());
175    }
176}