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