mod family;
mod hierarchical_policer;
mod policer;
mod prefix_action;
mod three_color_policer;
pub use family::{FilterAction, FilterActions, FilterKind, FilterTerm, FirewallFilter, MatchCondition};
pub use hierarchical_policer::{HierarchicalLimit, HierarchicalPolicer};
pub use policer::Policer;
pub use prefix_action::PrefixAction;
pub use three_color_policer::{ThreeColorPolicer, ThreeColorRate};
use crate::{named_children, top_level};
use asterust::config::Config;
use family::filters;
use hierarchical_policer::parse_hierarchical_policer;
use policer::parse_policer;
use prefix_action::prefix_actions;
use three_color_policer::parse_three_color_policer;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Firewall {
pub filters: Vec<FirewallFilter>,
pub policers: Vec<Policer>,
pub three_color_policers: Vec<ThreeColorPolicer>,
pub hierarchical_policers: Vec<HierarchicalPolicer>,
pub prefix_actions: Vec<PrefixAction>,
}
pub fn firewall(config: &Config) -> Option<Firewall> {
let root = top_level(config, "firewall")?;
Some(Firewall {
filters: filters(root),
policers: named_children(root, "policer")
.map(|(name, line)| parse_policer(name, line))
.collect(),
three_color_policers: named_children(root, "three-color-policer")
.map(|(name, line)| parse_three_color_policer(name, line))
.collect(),
hierarchical_policers: named_children(root, "hierarchical-policer")
.map(|(name, line)| parse_hierarchical_policer(name, line))
.collect(),
prefix_actions: prefix_actions(root),
})
}
#[cfg(test)]
mod tests {
use super::*;
use asterust::config::juniper;
fn parse(text: &str) -> Firewall {
firewall(&juniper::parse_braces(text)).expect("fixture has a firewall block")
}
const DOC_BGP179_PEERS: &str = r#"
firewall {
family inet {
filter filter_bgp179 {
term 1 {
from {
source-address {
10.10.10.2/32;
10.10.10.6/32;
}
destination-port bgp;
}
then accept;
}
term 2 {
then {
reject;
}
}
}
}
}
"#;
const DOC_BGP179_PREFIX_LIST: &str = r#"
firewall {
family inet {
filter filter_bgp179 {
term 1 {
from {
source-address {
0.0.0.0/0;
}
source-prefix-list {
plist_bgp179 except;
}
destination-port bgp;
}
then {
reject;
}
}
term 2 {
then {
accept;
}
}
}
}
}
"#;
#[test]
fn doc_filter_with_address_block_and_leaf_then() {
let firewall = parse(DOC_BGP179_PEERS);
assert_eq!(firewall.filters.len(), 1);
let filter = &firewall.filters[0];
assert_eq!(filter.family, "inet");
assert_eq!(filter.name, "filter_bgp179");
assert_eq!(filter.terms.len(), 2);
let term = &filter.terms[0];
assert_eq!(term.name, "1");
assert_eq!(
term.condition("source-address").unwrap().values,
["10.10.10.2/32", "10.10.10.6/32"]
);
assert_eq!(term.condition("destination-port").unwrap().values, ["bgp"]);
assert_eq!(term.then.action, Some(FilterAction::Accept));
}
#[test]
fn doc_term_without_from_and_block_then() {
let filter = &parse(DOC_BGP179_PEERS).filters[0];
assert!(filter.terms[1].from.is_empty());
assert_eq!(filter.terms[1].then.action, Some(FilterAction::Reject));
}
#[test]
fn doc_prefix_list_with_except_modifier() {
let filter = &parse(DOC_BGP179_PREFIX_LIST).filters[0];
let term = &filter.terms[0];
assert_eq!(term.condition("source-address").unwrap().values, ["0.0.0.0/0"]);
assert_eq!(
term.condition("source-prefix-list").unwrap().values,
["plist_bgp179 except"]
);
assert_eq!(term.then.action, Some(FilterAction::Reject));
assert_eq!(filter.terms[1].then.action, Some(FilterAction::Accept));
}
const CONFIG: &str = r#"
firewall {
family inet {
filter PROTECT-RE {
term ICMP {
from {
protocol icmp;
icmp-type [ echo-request echo-reply ];
first-fragment;
}
then {
policer ICMP-LIMIT;
count icmp-packets;
log;
accept;
}
}
term SSH {
from destination-port ssh;
then {
forwarding-class network-control;
loss-priority low;
next term;
}
}
inactive: term OLD {
then discard;
}
term DEFAULT {
then {
syslog;
discard;
}
}
}
}
family inet6 {
filter V6 {
term ALL {
then accept;
}
}
}
policer ICMP-LIMIT {
if-exceeding {
bandwidth-limit 1m;
burst-size-limit 15k;
}
then discard;
}
policer PERCENT {
if-exceeding {
bandwidth-percent 10;
burst-size-limit 10k;
}
then {
forwarding-class best-effort;
loss-priority high;
}
}
}
"#;
#[test]
fn lists_flags_and_nonterminating_actions() {
let firewall = parse(CONFIG);
let names: Vec<_> = firewall
.filters
.iter()
.map(|f| (f.family.as_str(), f.name.as_str()))
.collect();
assert_eq!(names, [("inet", "PROTECT-RE"), ("inet6", "V6")]);
let icmp = &firewall.filters[0].terms[0];
assert_eq!(icmp.condition("protocol").unwrap().values, ["icmp"]);
assert_eq!(
icmp.condition("icmp-type").unwrap().values,
["echo-request", "echo-reply"]
);
assert!(icmp.condition("first-fragment").unwrap().values.is_empty());
assert_eq!(icmp.then.policer.as_deref(), Some("ICMP-LIMIT"));
assert_eq!(icmp.then.count.as_deref(), Some("icmp-packets"));
assert!(icmp.then.log);
assert!(!icmp.then.syslog);
}
#[test]
fn leaf_from_next_term_and_skipped_inactive_term() {
let terms = &parse(CONFIG).filters[0].terms;
let names: Vec<_> = terms.iter().map(|t| t.name.as_str()).collect();
assert_eq!(names, ["ICMP", "SSH", "DEFAULT"]);
let ssh = &terms[1];
assert_eq!(ssh.condition("destination-port").unwrap().values, ["ssh"]);
assert_eq!(ssh.then.action, Some(FilterAction::NextTerm));
assert_eq!(ssh.then.forwarding_class.as_deref(), Some("network-control"));
assert_eq!(ssh.then.loss_priority.as_deref(), Some("low"));
assert_eq!(terms[2].then.action, Some(FilterAction::Discard));
assert!(terms[2].then.syslog);
}
#[test]
fn extracts_policers() {
let policers = parse(CONFIG).policers;
assert_eq!(policers.len(), 2);
assert_eq!(policers[0].name, "ICMP-LIMIT");
assert_eq!(policers[0].bandwidth_limit.as_deref(), Some("1m"));
assert_eq!(policers[0].burst_size_limit.as_deref(), Some("15k"));
assert_eq!(policers[0].bandwidth_percent, None);
assert!(policers[0].discard);
assert_eq!(policers[1].bandwidth_percent.as_deref(), Some("10"));
assert!(!policers[1].discard);
assert_eq!(policers[1].forwarding_class.as_deref(), Some("best-effort"));
assert_eq!(policers[1].loss_priority.as_deref(), Some("high"));
}
#[test]
fn no_firewall_is_none() {
assert!(firewall(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_none());
}
const DOC_ACTIONS: &str = r#"
firewall {
family inet {
prefix-action psa-1Mbps-per-source-24-32-256 {
policer 1Mbps-policer;
subnet-prefix-length 24;
source-prefix-length 32;
}
filter limit-source-one-24 {
term one {
from {
source-address {
10.10.10.0/24;
}
}
then prefix-action psa-1Mbps-per-source-24-32-256;
}
}
service-filter in_filter_presvc {
term t1 {
from {
protocol tcp;
source-port bgp;
}
then {
count svc_in_pkts;
service;
}
}
}
service-filter in_filter_postsvc {
term t2 {
from {
protocol tcp;
source-port bgp;
}
then {
count svc_in_pkts_rtn;
skip;
}
}
}
}
family inet {
filter sam {
term all {
then {
count count_sam;
sample;
}
}
}
filter "$upstream-inet" {
interface-specific;
term P0-Aggregate {
from {
dscp 46;
}
then {
hierarchical-policer "$P0-IN";
next term;
}
}
term P1-Premium {
from {
dscp [ 46 22 ];
}
then {
force-premium;
next term;
}
}
}
}
}
"#;
fn find<'a>(firewall: &'a Firewall, name: &str) -> &'a FirewallFilter {
firewall.filters.iter().find(|f| f.name == name).unwrap()
}
#[test]
fn reads_the_guide_prefix_action() {
let firewall = parse(DOC_ACTIONS);
let action = &firewall.prefix_actions[0];
assert_eq!(action.family, "inet");
assert_eq!(action.name, "psa-1Mbps-per-source-24-32-256");
assert_eq!(action.policer.as_deref(), Some("1Mbps-policer"));
assert_eq!(action.subnet_prefix_length.as_deref(), Some("24"));
assert_eq!(action.source_prefix_length.as_deref(), Some("32"));
assert!(!action.count);
assert_eq!(
find(&firewall, "limit-source-one-24").terms[0]
.then
.prefix_action
.as_deref(),
Some("psa-1Mbps-per-source-24-32-256")
);
}
#[test]
fn reads_the_guide_service_filters() {
let firewall = parse(DOC_ACTIONS);
let pre = find(&firewall, "in_filter_presvc");
assert_eq!(pre.kind, FilterKind::ServiceFilter);
assert_eq!(pre.family, "inet");
assert!(pre.terms[0].then.service);
assert_eq!(pre.terms[0].then.count.as_deref(), Some("svc_in_pkts"));
assert!(!pre.terms[0].then.skip);
let post = find(&firewall, "in_filter_postsvc");
assert!(post.terms[0].then.skip);
assert!(!post.terms[0].then.service);
assert_eq!(find(&firewall, "sam").kind, FilterKind::Filter);
}
#[test]
fn reads_the_guide_sample_and_hierarchical_policer() {
let firewall = parse(DOC_ACTIONS);
assert!(find(&firewall, "sam").terms[0].then.sample);
let upstream = find(&firewall, "$upstream-inet");
assert!(upstream.interface_specific);
assert_eq!(upstream.terms[0].then.hierarchical_policer.as_deref(), Some("$P0-IN"));
assert_eq!(upstream.terms[0].then.action, Some(FilterAction::NextTerm));
assert!(upstream.terms[1].then.force_premium);
assert_eq!(upstream.terms[1].from[0].values, ["46", "22"]);
}
#[test]
fn reads_the_other_actions_and_filter_statements() {
let firewall = parse(
r#"
firewall {
filter DIRECT {
accounting-profile PROFILE;
enhanced-mode;
fast-lookup-filter;
filter-list-template;
interface-shared;
physical-interface-filter;
promote gre-key;
term t {
then {
reject tcp-reset;
}
}
term u {
then {
bgp-output-queue-priority 5;
dont-fragment set;
dscp af11;
enhanced-hierarchical-policer;
ipsec-sa SA;
load-balance GROUP;
logical-system LS1;
next-hop-group NHG;
next-ip 192.0.2.1;
next-ip6 2001:db8::1;
packet-mode;
policy-map MAP;
port-mirror-instance MIRROR;
routing-instance VRF-A;
service-accounting;
service-filter-hit;
slice SLICE1;
source-class SC;
destination-class DC;
topology TOPO;
traffic-class 3;
exclude-accounting;
decapsulate gre routing-instance VRF-B;
encapsulate TEMPLATE;
}
}
term v {
then {
next-interface {
xe-0/1/0.1;
routing-instance rins1;
}
three-color-policer {
single-rate srTCM1;
}
}
}
term w {
then next-interface ge-0/0/0.0;
}
term x {
then port-mirror;
}
}
}
"#,
);
let filter = &firewall.filters[0];
assert_eq!(filter.family, "inet");
assert_eq!(filter.kind, FilterKind::Filter);
assert_eq!(filter.accounting_profile.as_deref(), Some("PROFILE"));
assert!(filter.enhanced_mode && filter.fast_lookup_filter && filter.filter_list_template);
assert!(filter.interface_shared && filter.physical_interface_filter && filter.promote_gre_key);
assert!(!filter.interface_specific);
let reject = &filter.terms[0].then;
assert_eq!(reject.action, Some(FilterAction::Reject));
assert_eq!(reject.reject_message.as_deref(), Some("tcp-reset"));
let then = &filter.terms[1].then;
assert_eq!(then.bgp_output_queue_priority.as_deref(), Some("5"));
assert_eq!(then.dont_fragment.as_deref(), Some("set"));
assert_eq!(then.dscp.as_deref(), Some("af11"));
assert!(then.enhanced_hierarchical_policer);
assert_eq!(then.ipsec_sa.as_deref(), Some("SA"));
assert_eq!(then.load_balance.as_deref(), Some("GROUP"));
assert_eq!(then.logical_system.as_deref(), Some("LS1"));
assert_eq!(then.next_hop_group.as_deref(), Some("NHG"));
assert_eq!(then.next_ip.as_deref(), Some("192.0.2.1"));
assert_eq!(then.next_ip6.as_deref(), Some("2001:db8::1"));
assert!(then.packet_mode);
assert_eq!(then.policy_map.as_deref(), Some("MAP"));
assert!(then.port_mirror);
assert_eq!(then.port_mirror_instance.as_deref(), Some("MIRROR"));
assert_eq!(then.routing_instance.as_deref(), Some("VRF-A"));
assert!(then.service_accounting && then.service_filter_hit && then.exclude_accounting);
assert_eq!(then.slice.as_deref(), Some("SLICE1"));
assert_eq!(then.source_class.as_deref(), Some("SC"));
assert_eq!(then.destination_class.as_deref(), Some("DC"));
assert_eq!(then.topology.as_deref(), Some("TOPO"));
assert_eq!(then.traffic_class.as_deref(), Some("3"));
assert_eq!(then.decapsulate.as_deref(), Some("gre routing-instance VRF-B"));
assert_eq!(then.encapsulate.as_deref(), Some("TEMPLATE"));
let block = &filter.terms[2].then;
assert_eq!(block.next_interface.as_deref(), Some("xe-0/1/0.1"));
assert_eq!(block.next_interface_routing_instance.as_deref(), Some("rins1"));
assert_eq!(block.three_color_policer.as_deref(), Some("single-rate srTCM1"));
assert_eq!(filter.terms[3].then.next_interface.as_deref(), Some("ge-0/0/0.0"));
assert!(filter.terms[4].then.port_mirror);
assert_eq!(filter.terms[4].then.port_mirror_instance, None);
}
#[test]
fn reads_simple_filters() {
let firewall = parse(
"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",
);
let filter = &firewall.filters[0];
assert_eq!(filter.kind, FilterKind::SimpleFilter);
assert_eq!(
filter.terms[0].then.forwarding_class.as_deref(),
Some("expedited-forwarding")
);
assert_eq!(filter.terms[0].then.loss_priority.as_deref(), Some("low"));
}
const DOC_POLICERS: &str = r#"
firewall {
three-color-policer srTCM1-ca {
action {
loss-priority high then discard;
}
single-rate {
color-aware;
committed-information-rate 40m;
committed-burst-size 100k;
excess-burst-size 200k;
}
}
three-color-policer trTCM2-cb {
logical-interface-policer;
action {
loss-priority high then discard;
}
two-rate {
color-blind;
committed-information-rate 40m;
committed-burst-size 100k;
peak-information-rate 60m;
peak-burst-size 200k;
}
}
policer police_pps {
if-exceeding-pps {
pps-limit 1k;
packet-burst 150;
}
then discard;
}
policer aggregate_police1 {
logical-interface-policer;
if-exceeding {
bandwidth-limit 100m;
burst-size-limit 500k;
}
then {
discard;
}
}
hierarchical-policer policer1 {
aggregate {
if-exceeding {
bandwidth-limit 300m;
burst-size-limit 30k;
}
then {
forwarding-class fc1;
}
}
premium {
if-exceeding {
bandwidth-limit 100m;
burst-size-limit 50k;
}
then {
discard;
}
}
}
}
"#;
#[test]
fn reads_the_guide_three_color_policers() {
let firewall = parse(DOC_POLICERS);
assert_eq!(firewall.three_color_policers.len(), 2);
let single = &firewall.three_color_policers[0];
assert_eq!(single.name, "srTCM1-ca");
assert_eq!(single.actions, ["loss-priority high then discard"]);
assert!(!single.logical_interface_policer);
assert!(single.two_rate.is_none());
let rate = single.single_rate.as_ref().unwrap();
assert_eq!(rate.color_aware, Some(true));
assert_eq!(rate.committed_information_rate.as_deref(), Some("40m"));
assert_eq!(rate.committed_burst_size.as_deref(), Some("100k"));
assert_eq!(rate.excess_burst_size.as_deref(), Some("200k"));
assert_eq!(rate.peak_information_rate, None);
let two = &firewall.three_color_policers[1];
assert!(two.logical_interface_policer);
assert!(two.single_rate.is_none());
let rate = two.two_rate.as_ref().unwrap();
assert_eq!(rate.color_aware, Some(false));
assert_eq!(rate.peak_information_rate.as_deref(), Some("60m"));
assert_eq!(rate.peak_burst_size.as_deref(), Some("200k"));
assert_eq!(rate.excess_burst_size, None);
}
#[test]
fn reads_the_guide_packet_rate_and_interface_policers() {
let firewall = parse(DOC_POLICERS);
let pps = &firewall.policers[0];
assert_eq!(pps.name, "police_pps");
assert_eq!(pps.pps_limit.as_deref(), Some("1k"));
assert_eq!(pps.packet_burst.as_deref(), Some("150"));
assert_eq!(pps.bandwidth_limit, None);
assert!(pps.discard);
assert!(!pps.logical_interface_policer);
let aggregate = &firewall.policers[1];
assert!(aggregate.logical_interface_policer);
assert_eq!(aggregate.bandwidth_limit.as_deref(), Some("100m"));
assert_eq!(aggregate.pps_limit, None);
}
#[test]
fn reads_the_guide_hierarchical_policer() {
let firewall = parse(DOC_POLICERS);
let policer = &firewall.hierarchical_policers[0];
assert_eq!(policer.name, "policer1");
let aggregate = policer.aggregate.as_ref().unwrap();
assert_eq!(aggregate.bandwidth_limit.as_deref(), Some("300m"));
assert_eq!(aggregate.burst_size_limit.as_deref(), Some("30k"));
assert_eq!(aggregate.forwarding_class.as_deref(), Some("fc1"));
assert!(!aggregate.discard);
let premium = policer.premium.as_ref().unwrap();
assert_eq!(premium.bandwidth_limit.as_deref(), Some("100m"));
assert!(premium.discard);
}
#[test]
fn reads_the_other_policer_statements() {
let firewall = parse(
r#"
firewall {
policer P {
filter-specific;
physical-interface-policer;
logical-bandwidth-policer;
shared-bandwidth-policer;
if-exceeding {
bandwidth-percent 20;
}
}
three-color-policer T {
filter-specific;
physical-interface-policer;
shared-bandwidth-policer;
}
hierarchical-policer H {
aggregate {
if-exceeding-pps {
pps-limit 5k;
packet-burst 100;
}
then {
loss-priority low;
}
}
}
}
"#,
);
let policer = &firewall.policers[0];
assert!(policer.filter_specific && policer.physical_interface_policer);
assert!(policer.logical_bandwidth_policer && policer.shared_bandwidth_policer);
assert!(!policer.logical_interface_policer);
let three = &firewall.three_color_policers[0];
assert!(three.filter_specific && three.physical_interface_policer && three.shared_bandwidth_policer);
assert!(three.actions.is_empty());
assert!(three.single_rate.is_none());
let aggregate = firewall.hierarchical_policers[0].aggregate.as_ref().unwrap();
assert_eq!(aggregate.pps_limit.as_deref(), Some("5k"));
assert_eq!(aggregate.packet_burst.as_deref(), Some("100"));
assert_eq!(aggregate.loss_priority.as_deref(), Some("low"));
assert!(firewall.hierarchical_policers[0].premium.is_none());
}
}