1mod family;
9mod hierarchical_policer;
10mod policer;
11mod prefix_action;
12mod three_color_policer;
13
14pub use family::{FilterAction, FilterActions, FilterKind, FilterTerm, FirewallFilter, MatchCondition};
15pub use hierarchical_policer::{HierarchicalLimit, HierarchicalPolicer};
16pub use policer::Policer;
17pub use prefix_action::PrefixAction;
18pub use three_color_policer::{ThreeColorPolicer, ThreeColorRate};
19
20use crate::{named_children, top_level};
21use asterust::config::Config;
22use family::filters;
23use hierarchical_policer::parse_hierarchical_policer;
24use policer::parse_policer;
25use prefix_action::prefix_actions;
26use three_color_policer::parse_three_color_policer;
27
28#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct Firewall {
32 pub filters: Vec<FirewallFilter>,
33 pub policers: Vec<Policer>,
34 pub three_color_policers: Vec<ThreeColorPolicer>,
35 pub hierarchical_policers: Vec<HierarchicalPolicer>,
36 pub prefix_actions: Vec<PrefixAction>,
37}
38
39pub fn firewall(config: &Config) -> Option<Firewall> {
53 let root = top_level(config, "firewall")?;
54 Some(Firewall {
55 filters: filters(root),
56 policers: named_children(root, "policer")
57 .map(|(name, line)| parse_policer(name, line))
58 .collect(),
59 three_color_policers: named_children(root, "three-color-policer")
60 .map(|(name, line)| parse_three_color_policer(name, line))
61 .collect(),
62 hierarchical_policers: named_children(root, "hierarchical-policer")
63 .map(|(name, line)| parse_hierarchical_policer(name, line))
64 .collect(),
65 prefix_actions: prefix_actions(root),
66 })
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72 use asterust::config::juniper;
73
74 fn parse(text: &str) -> Firewall {
75 firewall(&juniper::parse_braces(text)).expect("fixture has a firewall block")
76 }
77
78 const DOC_BGP179_PEERS: &str = r#"
81firewall {
82 family inet {
83 filter filter_bgp179 {
84 term 1 {
85 from {
86 source-address {
87 10.10.10.2/32;
88 10.10.10.6/32;
89 }
90 destination-port bgp;
91 }
92 then accept;
93 }
94 term 2 {
95 then {
96 reject;
97 }
98 }
99 }
100 }
101}
102"#;
103
104 const DOC_BGP179_PREFIX_LIST: &str = r#"
105firewall {
106 family inet {
107 filter filter_bgp179 {
108 term 1 {
109 from {
110 source-address {
111 0.0.0.0/0;
112 }
113 source-prefix-list {
114 plist_bgp179 except;
115 }
116 destination-port bgp;
117 }
118 then {
119 reject;
120 }
121 }
122 term 2 {
123 then {
124 accept;
125 }
126 }
127 }
128 }
129}
130"#;
131
132 #[test]
133 fn doc_filter_with_address_block_and_leaf_then() {
134 let firewall = parse(DOC_BGP179_PEERS);
135 assert_eq!(firewall.filters.len(), 1);
136 let filter = &firewall.filters[0];
137 assert_eq!(filter.family, "inet");
138 assert_eq!(filter.name, "filter_bgp179");
139 assert_eq!(filter.terms.len(), 2);
140
141 let term = &filter.terms[0];
142 assert_eq!(term.name, "1");
143 assert_eq!(
144 term.condition("source-address").unwrap().values,
145 ["10.10.10.2/32", "10.10.10.6/32"]
146 );
147 assert_eq!(term.condition("destination-port").unwrap().values, ["bgp"]);
148 assert_eq!(term.then.action, Some(FilterAction::Accept));
149 }
150
151 #[test]
152 fn doc_term_without_from_and_block_then() {
153 let filter = &parse(DOC_BGP179_PEERS).filters[0];
154 assert!(filter.terms[1].from.is_empty());
155 assert_eq!(filter.terms[1].then.action, Some(FilterAction::Reject));
156 }
157
158 #[test]
159 fn doc_prefix_list_with_except_modifier() {
160 let filter = &parse(DOC_BGP179_PREFIX_LIST).filters[0];
161 let term = &filter.terms[0];
162 assert_eq!(term.condition("source-address").unwrap().values, ["0.0.0.0/0"]);
163 assert_eq!(
164 term.condition("source-prefix-list").unwrap().values,
165 ["plist_bgp179 except"]
166 );
167 assert_eq!(term.then.action, Some(FilterAction::Reject));
168 assert_eq!(filter.terms[1].then.action, Some(FilterAction::Accept));
169 }
170
171 const CONFIG: &str = r#"
173firewall {
174 family inet {
175 filter PROTECT-RE {
176 term ICMP {
177 from {
178 protocol icmp;
179 icmp-type [ echo-request echo-reply ];
180 first-fragment;
181 }
182 then {
183 policer ICMP-LIMIT;
184 count icmp-packets;
185 log;
186 accept;
187 }
188 }
189 term SSH {
190 from destination-port ssh;
191 then {
192 forwarding-class network-control;
193 loss-priority low;
194 next term;
195 }
196 }
197 inactive: term OLD {
198 then discard;
199 }
200 term DEFAULT {
201 then {
202 syslog;
203 discard;
204 }
205 }
206 }
207 }
208 family inet6 {
209 filter V6 {
210 term ALL {
211 then accept;
212 }
213 }
214 }
215 policer ICMP-LIMIT {
216 if-exceeding {
217 bandwidth-limit 1m;
218 burst-size-limit 15k;
219 }
220 then discard;
221 }
222 policer PERCENT {
223 if-exceeding {
224 bandwidth-percent 10;
225 burst-size-limit 10k;
226 }
227 then {
228 forwarding-class best-effort;
229 loss-priority high;
230 }
231 }
232}
233"#;
234
235 #[test]
236 fn lists_flags_and_nonterminating_actions() {
237 let firewall = parse(CONFIG);
238 let names: Vec<_> = firewall
239 .filters
240 .iter()
241 .map(|f| (f.family.as_str(), f.name.as_str()))
242 .collect();
243 assert_eq!(names, [("inet", "PROTECT-RE"), ("inet6", "V6")]);
244
245 let icmp = &firewall.filters[0].terms[0];
246 assert_eq!(icmp.condition("protocol").unwrap().values, ["icmp"]);
247 assert_eq!(
248 icmp.condition("icmp-type").unwrap().values,
249 ["echo-request", "echo-reply"]
250 );
251 assert!(icmp.condition("first-fragment").unwrap().values.is_empty());
252 assert_eq!(icmp.then.policer.as_deref(), Some("ICMP-LIMIT"));
253 assert_eq!(icmp.then.count.as_deref(), Some("icmp-packets"));
254 assert!(icmp.then.log);
255 assert!(!icmp.then.syslog);
256 }
257
258 #[test]
259 fn leaf_from_next_term_and_skipped_inactive_term() {
260 let terms = &parse(CONFIG).filters[0].terms;
261 let names: Vec<_> = terms.iter().map(|t| t.name.as_str()).collect();
262 assert_eq!(names, ["ICMP", "SSH", "DEFAULT"]);
263
264 let ssh = &terms[1];
265 assert_eq!(ssh.condition("destination-port").unwrap().values, ["ssh"]);
266 assert_eq!(ssh.then.action, Some(FilterAction::NextTerm));
267 assert_eq!(ssh.then.forwarding_class.as_deref(), Some("network-control"));
268 assert_eq!(ssh.then.loss_priority.as_deref(), Some("low"));
269
270 assert_eq!(terms[2].then.action, Some(FilterAction::Discard));
271 assert!(terms[2].then.syslog);
272 }
273
274 #[test]
275 fn extracts_policers() {
276 let policers = parse(CONFIG).policers;
277 assert_eq!(policers.len(), 2);
278
279 assert_eq!(policers[0].name, "ICMP-LIMIT");
280 assert_eq!(policers[0].bandwidth_limit.as_deref(), Some("1m"));
281 assert_eq!(policers[0].burst_size_limit.as_deref(), Some("15k"));
282 assert_eq!(policers[0].bandwidth_percent, None);
283 assert!(policers[0].discard);
284
285 assert_eq!(policers[1].bandwidth_percent.as_deref(), Some("10"));
286 assert!(!policers[1].discard);
287 assert_eq!(policers[1].forwarding_class.as_deref(), Some("best-effort"));
288 assert_eq!(policers[1].loss_priority.as_deref(), Some("high"));
289 }
290
291 #[test]
292 fn no_firewall_is_none() {
293 assert!(firewall(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_none());
294 }
295
296 const DOC_ACTIONS: &str = r#"
300firewall {
301 family inet {
302 prefix-action psa-1Mbps-per-source-24-32-256 {
303 policer 1Mbps-policer;
304 subnet-prefix-length 24;
305 source-prefix-length 32;
306 }
307 filter limit-source-one-24 {
308 term one {
309 from {
310 source-address {
311 10.10.10.0/24;
312 }
313 }
314 then prefix-action psa-1Mbps-per-source-24-32-256;
315 }
316 }
317 service-filter in_filter_presvc {
318 term t1 {
319 from {
320 protocol tcp;
321 source-port bgp;
322 }
323 then {
324 count svc_in_pkts;
325 service;
326 }
327 }
328 }
329 service-filter in_filter_postsvc {
330 term t2 {
331 from {
332 protocol tcp;
333 source-port bgp;
334 }
335 then {
336 count svc_in_pkts_rtn;
337 skip;
338 }
339 }
340 }
341 }
342 family inet {
343 filter sam {
344 term all {
345 then {
346 count count_sam;
347 sample;
348 }
349 }
350 }
351 filter "$upstream-inet" {
352 interface-specific;
353 term P0-Aggregate {
354 from {
355 dscp 46;
356 }
357 then {
358 hierarchical-policer "$P0-IN";
359 next term;
360 }
361 }
362 term P1-Premium {
363 from {
364 dscp [ 46 22 ];
365 }
366 then {
367 force-premium;
368 next term;
369 }
370 }
371 }
372 }
373}
374"#;
375
376 fn find<'a>(firewall: &'a Firewall, name: &str) -> &'a FirewallFilter {
377 firewall.filters.iter().find(|f| f.name == name).unwrap()
378 }
379
380 #[test]
381 fn reads_the_guide_prefix_action() {
382 let firewall = parse(DOC_ACTIONS);
383 let action = &firewall.prefix_actions[0];
384 assert_eq!(action.family, "inet");
385 assert_eq!(action.name, "psa-1Mbps-per-source-24-32-256");
386 assert_eq!(action.policer.as_deref(), Some("1Mbps-policer"));
387 assert_eq!(action.subnet_prefix_length.as_deref(), Some("24"));
388 assert_eq!(action.source_prefix_length.as_deref(), Some("32"));
389 assert!(!action.count);
390 assert_eq!(
391 find(&firewall, "limit-source-one-24").terms[0]
392 .then
393 .prefix_action
394 .as_deref(),
395 Some("psa-1Mbps-per-source-24-32-256")
396 );
397 }
398
399 #[test]
400 fn reads_the_guide_service_filters() {
401 let firewall = parse(DOC_ACTIONS);
402 let pre = find(&firewall, "in_filter_presvc");
403 assert_eq!(pre.kind, FilterKind::ServiceFilter);
404 assert_eq!(pre.family, "inet");
405 assert!(pre.terms[0].then.service);
406 assert_eq!(pre.terms[0].then.count.as_deref(), Some("svc_in_pkts"));
407 assert!(!pre.terms[0].then.skip);
408
409 let post = find(&firewall, "in_filter_postsvc");
410 assert!(post.terms[0].then.skip);
411 assert!(!post.terms[0].then.service);
412 assert_eq!(find(&firewall, "sam").kind, FilterKind::Filter);
413 }
414
415 #[test]
416 fn reads_the_guide_sample_and_hierarchical_policer() {
417 let firewall = parse(DOC_ACTIONS);
418 assert!(find(&firewall, "sam").terms[0].then.sample);
419
420 let upstream = find(&firewall, "$upstream-inet");
421 assert!(upstream.interface_specific);
422 assert_eq!(upstream.terms[0].then.hierarchical_policer.as_deref(), Some("$P0-IN"));
423 assert_eq!(upstream.terms[0].then.action, Some(FilterAction::NextTerm));
424 assert!(upstream.terms[1].then.force_premium);
425 assert_eq!(upstream.terms[1].from[0].values, ["46", "22"]);
426 }
427
428 #[test]
431 fn reads_the_other_actions_and_filter_statements() {
432 let firewall = parse(
433 r#"
434firewall {
435 filter DIRECT {
436 accounting-profile PROFILE;
437 enhanced-mode;
438 fast-lookup-filter;
439 filter-list-template;
440 interface-shared;
441 physical-interface-filter;
442 promote gre-key;
443 term t {
444 then {
445 reject tcp-reset;
446 }
447 }
448 term u {
449 then {
450 bgp-output-queue-priority 5;
451 dont-fragment set;
452 dscp af11;
453 enhanced-hierarchical-policer;
454 ipsec-sa SA;
455 load-balance GROUP;
456 logical-system LS1;
457 next-hop-group NHG;
458 next-ip 192.0.2.1;
459 next-ip6 2001:db8::1;
460 packet-mode;
461 policy-map MAP;
462 port-mirror-instance MIRROR;
463 routing-instance VRF-A;
464 service-accounting;
465 service-filter-hit;
466 slice SLICE1;
467 source-class SC;
468 destination-class DC;
469 topology TOPO;
470 traffic-class 3;
471 exclude-accounting;
472 decapsulate gre routing-instance VRF-B;
473 encapsulate TEMPLATE;
474 }
475 }
476 term v {
477 then {
478 next-interface {
479 xe-0/1/0.1;
480 routing-instance rins1;
481 }
482 three-color-policer {
483 single-rate srTCM1;
484 }
485 }
486 }
487 term w {
488 then next-interface ge-0/0/0.0;
489 }
490 term x {
491 then port-mirror;
492 }
493 }
494}
495"#,
496 );
497 let filter = &firewall.filters[0];
498 assert_eq!(filter.family, "inet");
499 assert_eq!(filter.kind, FilterKind::Filter);
500 assert_eq!(filter.accounting_profile.as_deref(), Some("PROFILE"));
501 assert!(filter.enhanced_mode && filter.fast_lookup_filter && filter.filter_list_template);
502 assert!(filter.interface_shared && filter.physical_interface_filter && filter.promote_gre_key);
503 assert!(!filter.interface_specific);
504
505 let reject = &filter.terms[0].then;
506 assert_eq!(reject.action, Some(FilterAction::Reject));
507 assert_eq!(reject.reject_message.as_deref(), Some("tcp-reset"));
508
509 let then = &filter.terms[1].then;
510 assert_eq!(then.bgp_output_queue_priority.as_deref(), Some("5"));
511 assert_eq!(then.dont_fragment.as_deref(), Some("set"));
512 assert_eq!(then.dscp.as_deref(), Some("af11"));
513 assert!(then.enhanced_hierarchical_policer);
514 assert_eq!(then.ipsec_sa.as_deref(), Some("SA"));
515 assert_eq!(then.load_balance.as_deref(), Some("GROUP"));
516 assert_eq!(then.logical_system.as_deref(), Some("LS1"));
517 assert_eq!(then.next_hop_group.as_deref(), Some("NHG"));
518 assert_eq!(then.next_ip.as_deref(), Some("192.0.2.1"));
519 assert_eq!(then.next_ip6.as_deref(), Some("2001:db8::1"));
520 assert!(then.packet_mode);
521 assert_eq!(then.policy_map.as_deref(), Some("MAP"));
522 assert!(then.port_mirror);
523 assert_eq!(then.port_mirror_instance.as_deref(), Some("MIRROR"));
524 assert_eq!(then.routing_instance.as_deref(), Some("VRF-A"));
525 assert!(then.service_accounting && then.service_filter_hit && then.exclude_accounting);
526 assert_eq!(then.slice.as_deref(), Some("SLICE1"));
527 assert_eq!(then.source_class.as_deref(), Some("SC"));
528 assert_eq!(then.destination_class.as_deref(), Some("DC"));
529 assert_eq!(then.topology.as_deref(), Some("TOPO"));
530 assert_eq!(then.traffic_class.as_deref(), Some("3"));
531 assert_eq!(then.decapsulate.as_deref(), Some("gre routing-instance VRF-B"));
532 assert_eq!(then.encapsulate.as_deref(), Some("TEMPLATE"));
533
534 let block = &filter.terms[2].then;
535 assert_eq!(block.next_interface.as_deref(), Some("xe-0/1/0.1"));
536 assert_eq!(block.next_interface_routing_instance.as_deref(), Some("rins1"));
537 assert_eq!(block.three_color_policer.as_deref(), Some("single-rate srTCM1"));
538
539 assert_eq!(filter.terms[3].then.next_interface.as_deref(), Some("ge-0/0/0.0"));
540 assert!(filter.terms[4].then.port_mirror);
541 assert_eq!(filter.terms[4].then.port_mirror_instance, None);
542 }
543
544 #[test]
546 fn reads_simple_filters() {
547 let firewall = parse(
548 "firewall {\n family inet {\n simple-filter SF {\n term t {\n from {\n source-address 10.0.0.0/8;\n }\n then {\n forwarding-class expedited-forwarding;\n loss-priority low;\n }\n }\n }\n }\n}\n",
549 );
550 let filter = &firewall.filters[0];
551 assert_eq!(filter.kind, FilterKind::SimpleFilter);
552 assert_eq!(
553 filter.terms[0].then.forwarding_class.as_deref(),
554 Some("expedited-forwarding")
555 );
556 assert_eq!(filter.terms[0].then.loss_priority.as_deref(), Some("low"));
557 }
558
559 const DOC_POLICERS: &str = r#"
564firewall {
565 three-color-policer srTCM1-ca {
566 action {
567 loss-priority high then discard;
568 }
569 single-rate {
570 color-aware;
571 committed-information-rate 40m;
572 committed-burst-size 100k;
573 excess-burst-size 200k;
574 }
575 }
576 three-color-policer trTCM2-cb {
577 logical-interface-policer;
578 action {
579 loss-priority high then discard;
580 }
581 two-rate {
582 color-blind;
583 committed-information-rate 40m;
584 committed-burst-size 100k;
585 peak-information-rate 60m;
586 peak-burst-size 200k;
587 }
588 }
589 policer police_pps {
590 if-exceeding-pps {
591 pps-limit 1k;
592 packet-burst 150;
593 }
594 then discard;
595 }
596 policer aggregate_police1 {
597 logical-interface-policer;
598 if-exceeding {
599 bandwidth-limit 100m;
600 burst-size-limit 500k;
601 }
602 then {
603 discard;
604 }
605 }
606 hierarchical-policer policer1 {
607 aggregate {
608 if-exceeding {
609 bandwidth-limit 300m;
610 burst-size-limit 30k;
611 }
612 then {
613 forwarding-class fc1;
614 }
615 }
616 premium {
617 if-exceeding {
618 bandwidth-limit 100m;
619 burst-size-limit 50k;
620 }
621 then {
622 discard;
623 }
624 }
625 }
626}
627"#;
628
629 #[test]
630 fn reads_the_guide_three_color_policers() {
631 let firewall = parse(DOC_POLICERS);
632 assert_eq!(firewall.three_color_policers.len(), 2);
633
634 let single = &firewall.three_color_policers[0];
635 assert_eq!(single.name, "srTCM1-ca");
636 assert_eq!(single.actions, ["loss-priority high then discard"]);
637 assert!(!single.logical_interface_policer);
638 assert!(single.two_rate.is_none());
639 let rate = single.single_rate.as_ref().unwrap();
640 assert_eq!(rate.color_aware, Some(true));
641 assert_eq!(rate.committed_information_rate.as_deref(), Some("40m"));
642 assert_eq!(rate.committed_burst_size.as_deref(), Some("100k"));
643 assert_eq!(rate.excess_burst_size.as_deref(), Some("200k"));
644 assert_eq!(rate.peak_information_rate, None);
645
646 let two = &firewall.three_color_policers[1];
647 assert!(two.logical_interface_policer);
648 assert!(two.single_rate.is_none());
649 let rate = two.two_rate.as_ref().unwrap();
650 assert_eq!(rate.color_aware, Some(false));
651 assert_eq!(rate.peak_information_rate.as_deref(), Some("60m"));
652 assert_eq!(rate.peak_burst_size.as_deref(), Some("200k"));
653 assert_eq!(rate.excess_burst_size, None);
654 }
655
656 #[test]
657 fn reads_the_guide_packet_rate_and_interface_policers() {
658 let firewall = parse(DOC_POLICERS);
659 let pps = &firewall.policers[0];
660 assert_eq!(pps.name, "police_pps");
661 assert_eq!(pps.pps_limit.as_deref(), Some("1k"));
662 assert_eq!(pps.packet_burst.as_deref(), Some("150"));
663 assert_eq!(pps.bandwidth_limit, None);
664 assert!(pps.discard);
665 assert!(!pps.logical_interface_policer);
666
667 let aggregate = &firewall.policers[1];
668 assert!(aggregate.logical_interface_policer);
669 assert_eq!(aggregate.bandwidth_limit.as_deref(), Some("100m"));
670 assert_eq!(aggregate.pps_limit, None);
671 }
672
673 #[test]
674 fn reads_the_guide_hierarchical_policer() {
675 let firewall = parse(DOC_POLICERS);
676 let policer = &firewall.hierarchical_policers[0];
677 assert_eq!(policer.name, "policer1");
678 let aggregate = policer.aggregate.as_ref().unwrap();
679 assert_eq!(aggregate.bandwidth_limit.as_deref(), Some("300m"));
680 assert_eq!(aggregate.burst_size_limit.as_deref(), Some("30k"));
681 assert_eq!(aggregate.forwarding_class.as_deref(), Some("fc1"));
682 assert!(!aggregate.discard);
683 let premium = policer.premium.as_ref().unwrap();
684 assert_eq!(premium.bandwidth_limit.as_deref(), Some("100m"));
685 assert!(premium.discard);
686 }
687
688 #[test]
690 fn reads_the_other_policer_statements() {
691 let firewall = parse(
692 r#"
693firewall {
694 policer P {
695 filter-specific;
696 physical-interface-policer;
697 logical-bandwidth-policer;
698 shared-bandwidth-policer;
699 if-exceeding {
700 bandwidth-percent 20;
701 }
702 }
703 three-color-policer T {
704 filter-specific;
705 physical-interface-policer;
706 shared-bandwidth-policer;
707 }
708 hierarchical-policer H {
709 aggregate {
710 if-exceeding-pps {
711 pps-limit 5k;
712 packet-burst 100;
713 }
714 then {
715 loss-priority low;
716 }
717 }
718 }
719}
720"#,
721 );
722 let policer = &firewall.policers[0];
723 assert!(policer.filter_specific && policer.physical_interface_policer);
724 assert!(policer.logical_bandwidth_policer && policer.shared_bandwidth_policer);
725 assert!(!policer.logical_interface_policer);
726
727 let three = &firewall.three_color_policers[0];
728 assert!(three.filter_specific && three.physical_interface_policer && three.shared_bandwidth_policer);
729 assert!(three.actions.is_empty());
730 assert!(three.single_rate.is_none());
731
732 let aggregate = firewall.hierarchical_policers[0].aggregate.as_ref().unwrap();
733 assert_eq!(aggregate.pps_limit.as_deref(), Some("5k"));
734 assert_eq!(aggregate.packet_burst.as_deref(), Some("100"));
735 assert_eq!(aggregate.loss_priority.as_deref(), Some("low"));
736 assert!(firewall.hierarchical_policers[0].premium.is_none());
737 }
738}