asterust_junos/firewall/
mod.rs1mod family;
9mod policer;
10
11pub use family::{FilterAction, FilterActions, FilterTerm, FirewallFilter, MatchCondition};
12pub use policer::Policer;
13
14use crate::{named_children, top_level};
15use asterust::config::Config;
16use family::filters;
17use policer::parse_policer;
18
19#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct Firewall {
24 pub filters: Vec<FirewallFilter>,
25 pub policers: Vec<Policer>,
26}
27
28pub fn firewall(config: &Config) -> Option<Firewall> {
42 let root = top_level(config, "firewall")?;
43 Some(Firewall {
44 filters: filters(root),
45 policers: named_children(root, "policer")
46 .map(|(name, line)| parse_policer(name, line))
47 .collect(),
48 })
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54 use asterust::config::juniper;
55
56 fn parse(text: &str) -> Firewall {
57 firewall(&juniper::parse_braces(text)).expect("fixture has a firewall block")
58 }
59
60 const DOC_BGP179_PEERS: &str = r#"
63firewall {
64 family inet {
65 filter filter_bgp179 {
66 term 1 {
67 from {
68 source-address {
69 10.10.10.2/32;
70 10.10.10.6/32;
71 }
72 destination-port bgp;
73 }
74 then accept;
75 }
76 term 2 {
77 then {
78 reject;
79 }
80 }
81 }
82 }
83}
84"#;
85
86 const DOC_BGP179_PREFIX_LIST: &str = r#"
87firewall {
88 family inet {
89 filter filter_bgp179 {
90 term 1 {
91 from {
92 source-address {
93 0.0.0.0/0;
94 }
95 source-prefix-list {
96 plist_bgp179 except;
97 }
98 destination-port bgp;
99 }
100 then {
101 reject;
102 }
103 }
104 term 2 {
105 then {
106 accept;
107 }
108 }
109 }
110 }
111}
112"#;
113
114 #[test]
115 fn doc_filter_with_address_block_and_leaf_then() {
116 let firewall = parse(DOC_BGP179_PEERS);
117 assert_eq!(firewall.filters.len(), 1);
118 let filter = &firewall.filters[0];
119 assert_eq!(filter.family, "inet");
120 assert_eq!(filter.name, "filter_bgp179");
121 assert_eq!(filter.terms.len(), 2);
122
123 let term = &filter.terms[0];
124 assert_eq!(term.name, "1");
125 assert_eq!(
126 term.condition("source-address").unwrap().values,
127 ["10.10.10.2/32", "10.10.10.6/32"]
128 );
129 assert_eq!(term.condition("destination-port").unwrap().values, ["bgp"]);
130 assert_eq!(term.then.action, Some(FilterAction::Accept));
131 }
132
133 #[test]
134 fn doc_term_without_from_and_block_then() {
135 let filter = &parse(DOC_BGP179_PEERS).filters[0];
136 assert!(filter.terms[1].from.is_empty());
137 assert_eq!(filter.terms[1].then.action, Some(FilterAction::Reject));
138 }
139
140 #[test]
141 fn doc_prefix_list_with_except_modifier() {
142 let filter = &parse(DOC_BGP179_PREFIX_LIST).filters[0];
143 let term = &filter.terms[0];
144 assert_eq!(term.condition("source-address").unwrap().values, ["0.0.0.0/0"]);
145 assert_eq!(
146 term.condition("source-prefix-list").unwrap().values,
147 ["plist_bgp179 except"]
148 );
149 assert_eq!(term.then.action, Some(FilterAction::Reject));
150 assert_eq!(filter.terms[1].then.action, Some(FilterAction::Accept));
151 }
152
153 const CONFIG: &str = r#"
155firewall {
156 family inet {
157 filter PROTECT-RE {
158 term ICMP {
159 from {
160 protocol icmp;
161 icmp-type [ echo-request echo-reply ];
162 first-fragment;
163 }
164 then {
165 policer ICMP-LIMIT;
166 count icmp-packets;
167 log;
168 accept;
169 }
170 }
171 term SSH {
172 from destination-port ssh;
173 then {
174 forwarding-class network-control;
175 loss-priority low;
176 next term;
177 }
178 }
179 inactive: term OLD {
180 then discard;
181 }
182 term DEFAULT {
183 then {
184 syslog;
185 discard;
186 }
187 }
188 }
189 }
190 family inet6 {
191 filter V6 {
192 term ALL {
193 then accept;
194 }
195 }
196 }
197 policer ICMP-LIMIT {
198 if-exceeding {
199 bandwidth-limit 1m;
200 burst-size-limit 15k;
201 }
202 then discard;
203 }
204 policer PERCENT {
205 if-exceeding {
206 bandwidth-percent 10;
207 burst-size-limit 10k;
208 }
209 then {
210 forwarding-class best-effort;
211 loss-priority high;
212 }
213 }
214}
215"#;
216
217 #[test]
218 fn lists_flags_and_nonterminating_actions() {
219 let firewall = parse(CONFIG);
220 let names: Vec<_> = firewall
221 .filters
222 .iter()
223 .map(|f| (f.family.as_str(), f.name.as_str()))
224 .collect();
225 assert_eq!(names, [("inet", "PROTECT-RE"), ("inet6", "V6")]);
226
227 let icmp = &firewall.filters[0].terms[0];
228 assert_eq!(icmp.condition("protocol").unwrap().values, ["icmp"]);
229 assert_eq!(
230 icmp.condition("icmp-type").unwrap().values,
231 ["echo-request", "echo-reply"]
232 );
233 assert!(icmp.condition("first-fragment").unwrap().values.is_empty());
234 assert_eq!(icmp.then.policer.as_deref(), Some("ICMP-LIMIT"));
235 assert_eq!(icmp.then.count.as_deref(), Some("icmp-packets"));
236 assert!(icmp.then.log);
237 assert!(!icmp.then.syslog);
238 }
239
240 #[test]
241 fn leaf_from_next_term_and_skipped_inactive_term() {
242 let terms = &parse(CONFIG).filters[0].terms;
243 let names: Vec<_> = terms.iter().map(|t| t.name.as_str()).collect();
244 assert_eq!(names, ["ICMP", "SSH", "DEFAULT"]);
245
246 let ssh = &terms[1];
247 assert_eq!(ssh.condition("destination-port").unwrap().values, ["ssh"]);
248 assert_eq!(ssh.then.action, Some(FilterAction::NextTerm));
249 assert_eq!(ssh.then.forwarding_class.as_deref(), Some("network-control"));
250 assert_eq!(ssh.then.loss_priority.as_deref(), Some("low"));
251
252 assert_eq!(terms[2].then.action, Some(FilterAction::Discard));
253 assert!(terms[2].then.syslog);
254 }
255
256 #[test]
257 fn extracts_policers() {
258 let policers = parse(CONFIG).policers;
259 assert_eq!(policers.len(), 2);
260
261 assert_eq!(policers[0].name, "ICMP-LIMIT");
262 assert_eq!(policers[0].bandwidth_limit.as_deref(), Some("1m"));
263 assert_eq!(policers[0].burst_size_limit.as_deref(), Some("15k"));
264 assert_eq!(policers[0].bandwidth_percent, None);
265 assert!(policers[0].discard);
266
267 assert_eq!(policers[1].bandwidth_percent.as_deref(), Some("10"));
268 assert!(!policers[1].discard);
269 assert_eq!(policers[1].forwarding_class.as_deref(), Some("best-effort"));
270 assert_eq!(policers[1].loss_priority.as_deref(), Some("high"));
271 }
272
273 #[test]
274 fn no_firewall_is_none() {
275 assert!(firewall(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_none());
276 }
277}