asterust_junos/firewall/family/
filter.rs1use crate::{active_children, list_values, named_children, split_keyword, statements, unquote};
4use asterust::config::ConfigLine;
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct MatchCondition {
10 pub name: String,
11 pub values: Vec<String>,
13}
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub enum FilterAction {
19 Accept,
20 Discard,
21 Reject,
22 NextTerm,
23}
24
25#[derive(Clone, Debug, Default, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct FilterActions {
29 pub action: Option<FilterAction>,
30 pub count: Option<String>,
32 pub policer: Option<String>,
33 pub forwarding_class: Option<String>,
34 pub loss_priority: Option<String>,
35 pub log: bool,
36 pub syslog: bool,
37}
38
39#[derive(Clone, Debug, PartialEq, Eq)]
41#[non_exhaustive]
42pub struct FilterTerm {
43 pub name: String,
44 pub from: Vec<MatchCondition>,
45 pub then: FilterActions,
46}
47
48impl FilterTerm {
49 pub fn condition(&self, name: &str) -> Option<&MatchCondition> {
51 self.from.iter().find(|c| c.name == name)
52 }
53}
54
55#[derive(Clone, Debug, PartialEq, Eq)]
57#[non_exhaustive]
58pub struct FirewallFilter {
59 pub family: String,
61 pub name: String,
62 pub terms: Vec<FilterTerm>,
63}
64
65pub(super) fn parse_filter(family: &str, name: &str, line: &ConfigLine) -> FirewallFilter {
66 FirewallFilter {
67 family: family.to_string(),
68 name: name.to_string(),
69 terms: named_children(line, "term")
70 .map(|(term_name, term)| FilterTerm {
71 name: term_name.to_string(),
72 from: parse_conditions(term),
73 then: parse_actions(term),
74 })
75 .collect(),
76 }
77}
78
79fn parse_conditions(term: &ConfigLine) -> Vec<MatchCondition> {
81 let mut found = Vec::new();
82 for child in active_children(term) {
83 if child.text == "from" {
84 found.extend(active_children(child).map(parse_condition));
85 } else if let Some(statement) = child.arg("from") {
86 found.push(condition_from(statement, &[]));
87 }
88 }
89 found
90}
91
92fn parse_condition(line: &ConfigLine) -> MatchCondition {
93 condition_from(&line.text, &line.children)
94}
95
96fn condition_from(statement: &str, children: &[ConfigLine]) -> MatchCondition {
97 let (name, arg) = split_keyword(statement);
98 let mut values = if arg.is_empty() {
100 Vec::new()
101 } else if arg.starts_with('[') {
102 list_values(arg)
103 } else {
104 vec![unquote(arg).to_string()]
105 };
106 values.extend(
107 children
108 .iter()
109 .filter(|c| !c.text.starts_with("inactive:"))
110 .map(|c| c.text.clone()),
111 );
112 MatchCondition {
113 name: name.to_string(),
114 values,
115 }
116}
117
118fn parse_actions(term: &ConfigLine) -> FilterActions {
119 let mut actions = FilterActions::default();
120 for statement in statements(term, "then") {
121 let (action, arg) = split_keyword(statement);
122 let value = || Some(unquote(arg).to_string());
123 match (action, arg) {
124 ("accept", _) => actions.action = Some(FilterAction::Accept),
125 ("discard", _) => actions.action = Some(FilterAction::Discard),
126 ("reject", _) => actions.action = Some(FilterAction::Reject),
127 ("next", "term") => actions.action = Some(FilterAction::NextTerm),
128 ("count", _) => actions.count = value(),
129 ("policer", _) => actions.policer = value(),
130 ("forwarding-class", _) => actions.forwarding_class = value(),
131 ("loss-priority", _) => actions.loss_priority = value(),
132 ("log", "") => actions.log = true,
133 ("syslog", "") => actions.syslog = true,
134 _ => {}
135 }
136 }
137 actions
138}