Skip to main content

asterust_junos/system/
internet_options.rs

1//! `system { internet-options { ... } }`.
2
3use crate::{arg_of, has_leaf, statement_words, toggle, 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/// `<keyword> bucket-size <n> packet-rate <n>`, on one line or as one child statement per option.
64fn rate_limit(options: &ConfigLine, keyword: &str) -> Option<IcmpRateLimit> {
65    let words = statement_words(options, keyword)?;
66    Some(IcmpRateLimit {
67        bucket_size: value_after(&words, "bucket-size"),
68        packet_rate: value_after(&words, "packet-rate"),
69    })
70}
71
72#[cfg(test)]
73mod tests {
74    use crate::system::system;
75    use asterust::config::juniper;
76
77    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
78    const INTERNET_OPTIONS: &str = r#"
79system {
80    internet-options {
81        icmpv4-rate-limit bucket-size 10 packet-rate 500;
82        icmpv6-rate-limit {
83            bucket-size 3;
84            packet-rate 200;
85        }
86        no-ipv6-reject-zero-hop-limit;
87        no-source-quench;
88        path-mtu-discovery;
89        no-ipv6-path-mtu-discovery;
90        no-tcp-reset drop-all-tcp;
91        no-tcp-rfc1323;
92        no-tcp-rfc1323-paws;
93        tcp-drop-synfin-set;
94        tcp-mss 1400;
95        ipv6-duplicate-addr-detection-transmits 5;
96        source-port {
97            upper-limit 40000;
98        }
99    }
100}
101"#;
102
103    #[test]
104    fn extracts_toggle_options() {
105        let options = system(&juniper::parse_braces(INTERNET_OPTIONS))
106            .unwrap()
107            .internet_options
108            .unwrap();
109        assert_eq!(options.path_mtu_discovery, Some(true));
110        assert_eq!(options.ipv6_path_mtu_discovery, Some(false));
111        assert_eq!(options.ipv6_reject_zero_hop_limit, Some(false));
112        assert_eq!(options.source_quench, Some(false));
113        assert_eq!(options.gre_path_mtu_discovery, None);
114        assert_eq!(options.ipip_path_mtu_discovery, None);
115    }
116
117    #[test]
118    fn extracts_icmp_rate_limits_in_line_and_block_form() {
119        let options = system(&juniper::parse_braces(INTERNET_OPTIONS))
120            .unwrap()
121            .internet_options
122            .unwrap();
123        let v4 = options.icmpv4_rate_limit.unwrap();
124        assert_eq!(v4.bucket_size.as_deref(), Some("10"));
125        assert_eq!(v4.packet_rate.as_deref(), Some("500"));
126        let v6 = options.icmpv6_rate_limit.unwrap();
127        assert_eq!(v6.bucket_size.as_deref(), Some("3"));
128        assert_eq!(v6.packet_rate.as_deref(), Some("200"));
129    }
130
131    #[test]
132    fn extracts_tcp_and_port_options() {
133        let options = system(&juniper::parse_braces(INTERNET_OPTIONS))
134            .unwrap()
135            .internet_options
136            .unwrap();
137        assert!(options.tcp_drop_synfin_set);
138        assert!(options.no_tcp_rfc1323);
139        assert!(options.no_tcp_rfc1323_paws);
140        assert_eq!(options.no_tcp_reset.as_deref(), Some("drop-all-tcp"));
141        assert_eq!(options.tcp_mss.as_deref(), Some("1400"));
142        assert_eq!(options.source_port_upper_limit.as_deref(), Some("40000"));
143        assert_eq!(options.ipv6_duplicate_addr_detection_transmits.as_deref(), Some("5"));
144    }
145
146    #[test]
147    fn unset_options_are_none_or_false() {
148        let options = system(&juniper::parse_braces(
149            "system {\n    internet-options {\n        tcp-mss 1300;\n    }\n}\n",
150        ))
151        .unwrap()
152        .internet_options
153        .unwrap();
154        assert_eq!(options.path_mtu_discovery, None);
155        assert_eq!(options.icmpv4_rate_limit, None);
156        assert!(!options.tcp_drop_synfin_set);
157        assert_eq!(options.source_port_upper_limit, None);
158    }
159
160    #[test]
161    fn no_internet_options_is_none() {
162        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
163        assert!(system.internet_options.is_none());
164    }
165}