asterust_junos/system/
internet.rs1use crate::{arg_of, has_leaf, statement_words};
4use asterust::config::ConfigLine;
5
6#[derive(Clone, Debug, Default, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct IcmpRateLimit {
10 pub bucket_size: Option<String>,
12 pub packet_rate: Option<String>,
14}
15
16#[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 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
63fn 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
74fn 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
83fn value_after(words: &[&str], name: &str) -> Option<String> {
84 let position = words.iter().position(|w| *w == name)?;
85 words.get(position + 1).map(|w| w.to_string())
86}
87
88#[cfg(test)]
89mod tests {
90 use crate::system::system;
91 use asterust::config::juniper;
92
93 const INTERNET_OPTIONS: &str = r#"
95system {
96 internet-options {
97 icmpv4-rate-limit bucket-size 10 packet-rate 500;
98 icmpv6-rate-limit {
99 bucket-size 3;
100 packet-rate 200;
101 }
102 no-ipv6-reject-zero-hop-limit;
103 no-source-quench;
104 path-mtu-discovery;
105 no-ipv6-path-mtu-discovery;
106 no-tcp-reset drop-all-tcp;
107 no-tcp-rfc1323;
108 no-tcp-rfc1323-paws;
109 tcp-drop-synfin-set;
110 tcp-mss 1400;
111 ipv6-duplicate-addr-detection-transmits 5;
112 source-port {
113 upper-limit 40000;
114 }
115 }
116}
117"#;
118
119 #[test]
120 fn extracts_toggle_options() {
121 let options = system(&juniper::parse_braces(INTERNET_OPTIONS))
122 .unwrap()
123 .internet_options
124 .unwrap();
125 assert_eq!(options.path_mtu_discovery, Some(true));
126 assert_eq!(options.ipv6_path_mtu_discovery, Some(false));
127 assert_eq!(options.ipv6_reject_zero_hop_limit, Some(false));
128 assert_eq!(options.source_quench, Some(false));
129 assert_eq!(options.gre_path_mtu_discovery, None);
130 assert_eq!(options.ipip_path_mtu_discovery, None);
131 }
132
133 #[test]
134 fn extracts_icmp_rate_limits_in_line_and_block_form() {
135 let options = system(&juniper::parse_braces(INTERNET_OPTIONS))
136 .unwrap()
137 .internet_options
138 .unwrap();
139 let v4 = options.icmpv4_rate_limit.unwrap();
140 assert_eq!(v4.bucket_size.as_deref(), Some("10"));
141 assert_eq!(v4.packet_rate.as_deref(), Some("500"));
142 let v6 = options.icmpv6_rate_limit.unwrap();
143 assert_eq!(v6.bucket_size.as_deref(), Some("3"));
144 assert_eq!(v6.packet_rate.as_deref(), Some("200"));
145 }
146
147 #[test]
148 fn extracts_tcp_and_port_options() {
149 let options = system(&juniper::parse_braces(INTERNET_OPTIONS))
150 .unwrap()
151 .internet_options
152 .unwrap();
153 assert!(options.tcp_drop_synfin_set);
154 assert!(options.no_tcp_rfc1323);
155 assert!(options.no_tcp_rfc1323_paws);
156 assert_eq!(options.no_tcp_reset.as_deref(), Some("drop-all-tcp"));
157 assert_eq!(options.tcp_mss.as_deref(), Some("1400"));
158 assert_eq!(options.source_port_upper_limit.as_deref(), Some("40000"));
159 assert_eq!(options.ipv6_duplicate_addr_detection_transmits.as_deref(), Some("5"));
160 }
161
162 #[test]
163 fn unset_options_are_none_or_false() {
164 let options = system(&juniper::parse_braces(
165 "system {\n internet-options {\n tcp-mss 1300;\n }\n}\n",
166 ))
167 .unwrap()
168 .internet_options
169 .unwrap();
170 assert_eq!(options.path_mtu_discovery, None);
171 assert_eq!(options.icmpv4_rate_limit, None);
172 assert!(!options.tcp_drop_synfin_set);
173 assert_eq!(options.source_port_upper_limit, None);
174 }
175
176 #[test]
177 fn no_internet_options_is_none() {
178 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
179 assert!(system.internet_options.is_none());
180 }
181}