1use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
27use crate::{active_children, arg_of, child_block, has_leaf, named_children};
28use asterust::config::ConfigLine;
29
30#[derive(Clone, Debug, Default, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct DdosFlowLevels {
35 pub logical_interface: Option<String>,
36 pub physical_interface: Option<String>,
37 pub subscriber: Option<String>,
38}
39
40#[derive(Clone, Debug, PartialEq, Eq)]
42#[non_exhaustive]
43pub struct DdosFpc {
44 pub slot: String,
45 pub bandwidth_scale: Option<String>,
46 pub burst_scale: Option<String>,
47 pub disable_fpc: bool,
48 pub hostbound_queue: Option<String>,
49}
50
51#[derive(Clone, Debug, PartialEq, Eq)]
53#[non_exhaustive]
54pub struct DdosPacketType {
55 pub name: String,
57 pub bandwidth: Option<String>,
59 pub burst: Option<String>,
61 pub bypass_aggregate: bool,
62 pub disable_fpc: bool,
63 pub disable_logging: bool,
64 pub disable_routing_engine: bool,
65 pub fpcs: Vec<DdosFpc>,
66 pub hostbound_queue: Option<String>,
67 pub priority: Option<String>,
69 pub recover_time: Option<String>,
71 pub range: Option<String>,
73 pub flow_detection_mode: Option<String>,
75 pub flow_detect_time: Option<String>,
76 pub flow_recover_time: Option<String>,
77 pub flow_timeout_time: Option<String>,
78 pub flow_level_bandwidth: Option<DdosFlowLevels>,
79 pub flow_level_control: Option<DdosFlowLevels>,
80 pub flow_level_detection: Option<DdosFlowLevels>,
81 pub no_flow_logging: bool,
82 pub timeout_active_flows: bool,
83}
84
85#[derive(Clone, Debug, PartialEq, Eq)]
87#[non_exhaustive]
88pub struct DdosProtocolGroup {
89 pub name: String,
90 pub packet_types: Vec<DdosPacketType>,
92}
93
94#[derive(Clone, Debug, PartialEq, Eq)]
96#[non_exhaustive]
97pub struct DdosGlobal {
98 pub disable_fpc: bool,
99 pub disable_logging: bool,
100 pub disable_routing_engine: bool,
101 pub flow_detection: bool,
102 pub flow_detection_mode: Option<String>,
103 pub flow_level_control: Option<String>,
104 pub flow_report_rate: Option<String>,
106 pub violation_report_rate: Option<String>,
108}
109
110#[derive(Clone, Debug, PartialEq, Eq)]
112#[non_exhaustive]
113pub struct DdosProtection {
114 pub global: Option<DdosGlobal>,
115 pub protocols: Vec<DdosProtocolGroup>,
116 pub traceoptions: Option<Traceoptions>,
117}
118
119fn levels(parent: &ConfigLine, keyword: &str) -> Option<DdosFlowLevels> {
120 let block = child_block(parent, keyword)?;
121 let level = |name: &str| arg_of(block, name).map(str::to_string);
122 Some(DdosFlowLevels {
123 logical_interface: level("logical-interface"),
124 physical_interface: level("physical-interface"),
125 subscriber: level("subscriber"),
126 })
127}
128
129fn parse_packet_type(name: &str, packet: &ConfigLine) -> DdosPacketType {
130 let flow = child_block(packet, "flow-detection");
133 let flow_field = |keyword: &str| {
134 arg_of(packet, keyword)
135 .or_else(|| flow.and_then(|f| arg_of(f, keyword)))
136 .map(str::to_string)
137 };
138 let flow_levels = |keyword: &str| levels(packet, keyword).or_else(|| flow.and_then(|f| levels(f, keyword)));
139 let flow_flag = |keyword: &str| has_leaf(packet, keyword) || flow.is_some_and(|f| has_leaf(f, keyword));
140 DdosPacketType {
141 name: name.to_string(),
142 bandwidth: arg_of(packet, "bandwidth").map(str::to_string),
143 burst: arg_of(packet, "burst").map(str::to_string),
144 bypass_aggregate: has_leaf(packet, "bypass-aggregate"),
145 disable_fpc: has_leaf(packet, "disable-fpc"),
146 disable_logging: has_leaf(packet, "disable-logging"),
147 disable_routing_engine: has_leaf(packet, "disable-routing-engine"),
148 fpcs: named_children(packet, "fpc")
149 .map(|(slot, fpc)| DdosFpc {
150 slot: slot.to_string(),
151 bandwidth_scale: arg_of(fpc, "bandwidth-scale").map(str::to_string),
152 burst_scale: arg_of(fpc, "burst-scale").map(str::to_string),
153 disable_fpc: has_leaf(fpc, "disable-fpc"),
154 hostbound_queue: arg_of(fpc, "hostbound-queue").map(str::to_string),
155 })
156 .collect(),
157 hostbound_queue: arg_of(packet, "hostbound-queue").map(str::to_string),
158 priority: arg_of(packet, "priority").map(str::to_string),
159 recover_time: arg_of(packet, "recover-time").map(str::to_string),
160 range: arg_of(packet, "range").map(str::to_string),
161 flow_detection_mode: flow_field("flow-detection-mode"),
162 flow_detect_time: flow_field("flow-detect-time"),
163 flow_recover_time: flow_field("flow-recover-time"),
164 flow_timeout_time: flow_field("flow-timeout-time"),
165 flow_level_bandwidth: flow_levels("flow-level-bandwidth"),
166 flow_level_control: flow_levels("flow-level-control"),
167 flow_level_detection: flow_levels("flow-level-detection"),
168 no_flow_logging: flow_flag("no-flow-logging"),
169 timeout_active_flows: flow_flag("timeout-active-flows"),
170 }
171}
172
173pub(super) fn parse_ddos_protection(ddos: &ConfigLine) -> DdosProtection {
174 DdosProtection {
175 global: child_block(ddos, "global").map(|global| DdosGlobal {
176 disable_fpc: has_leaf(global, "disable-fpc"),
177 disable_logging: has_leaf(global, "disable-logging"),
178 disable_routing_engine: has_leaf(global, "disable-routing-engine"),
179 flow_detection: has_leaf(global, "flow-detection"),
180 flow_detection_mode: arg_of(global, "flow-detection-mode").map(str::to_string),
181 flow_level_control: arg_of(global, "flow-level-control").map(str::to_string),
182 flow_report_rate: arg_of(global, "flow-report-rate").map(str::to_string),
183 violation_report_rate: arg_of(global, "violation-report-rate").map(str::to_string),
184 }),
185 protocols: child_block(ddos, "protocols")
186 .map(|protocols| {
187 active_children(protocols)
188 .map(|group| DdosProtocolGroup {
189 name: group.text.clone(),
190 packet_types: active_children(group)
191 .map(|packet| parse_packet_type(&packet.text, packet))
192 .collect(),
193 })
194 .collect()
195 })
196 .unwrap_or_default(),
197 traceoptions: parse_traceoptions(ddos),
198 }
199}
200
201#[cfg(test)]
202mod tests {
203 use crate::system::system;
204 use asterust::config::juniper;
205
206 #[test]
208 fn extracts_the_global_settings() {
209 let ddos = system(&juniper::parse_braces(
210 r#"
211system {
212 ddos-protection {
213 global {
214 disable-fpc;
215 disable-logging;
216 disable-routing-engine;
217 flow-detection;
218 flow-detection-mode automatic;
219 flow-level-control police;
220 flow-report-rate 50;
221 violation-report-rate 200;
222 }
223 traceoptions {
224 file ddos.log;
225 flag all;
226 }
227 }
228}
229"#,
230 ))
231 .unwrap()
232 .ddos_protection
233 .unwrap();
234 let global = ddos.global.unwrap();
235 assert!(global.disable_fpc);
236 assert!(global.disable_logging);
237 assert!(global.disable_routing_engine);
238 assert!(global.flow_detection);
239 assert_eq!(global.flow_detection_mode.as_deref(), Some("automatic"));
240 assert_eq!(global.flow_level_control.as_deref(), Some("police"));
241 assert_eq!(global.flow_report_rate.as_deref(), Some("50"));
242 assert_eq!(global.violation_report_rate.as_deref(), Some("200"));
243 assert_eq!(ddos.traceoptions.unwrap().file.as_deref(), Some("ddos.log"));
244 assert!(ddos.protocols.is_empty());
245 }
246
247 #[test]
248 fn extracts_protocol_groups_and_packet_types() {
249 let ddos = system(&juniper::parse_braces(
250 r#"
251system {
252 ddos-protection {
253 protocols {
254 pppoe {
255 aggregate {
256 bandwidth 8000;
257 burst 2000;
258 recover-time 120;
259 fpc 2 {
260 bandwidth-scale 50;
261 burst-scale 25;
262 disable-fpc;
263 }
264 }
265 padi {
266 bandwidth 500;
267 bypass-aggregate;
268 disable-logging;
269 priority medium;
270 hostbound-queue 3;
271 flow-detection-mode on;
272 flow-detect-time 5;
273 flow-recover-time 90;
274 flow-timeout-time 600;
275 no-flow-logging;
276 timeout-active-flows;
277 flow-level-bandwidth {
278 logical-interface 100;
279 subscriber 10;
280 }
281 flow-level-control {
282 physical-interface drop;
283 }
284 flow-level-detection {
285 subscriber off;
286 }
287 }
288 }
289 sample {
290 aggregate {
291 range 5000;
292 }
293 }
294 }
295 }
296}
297"#,
298 ))
299 .unwrap()
300 .ddos_protection
301 .unwrap();
302 assert_eq!(ddos.protocols.len(), 2);
303 let pppoe = &ddos.protocols[0];
304 assert_eq!(pppoe.name, "pppoe");
305 assert_eq!(pppoe.packet_types.len(), 2);
306
307 let aggregate = &pppoe.packet_types[0];
308 assert_eq!(aggregate.name, "aggregate");
309 assert_eq!(aggregate.bandwidth.as_deref(), Some("8000"));
310 assert_eq!(aggregate.burst.as_deref(), Some("2000"));
311 assert_eq!(aggregate.recover_time.as_deref(), Some("120"));
312 assert_eq!(aggregate.fpcs[0].slot, "2");
313 assert_eq!(aggregate.fpcs[0].bandwidth_scale.as_deref(), Some("50"));
314 assert_eq!(aggregate.fpcs[0].burst_scale.as_deref(), Some("25"));
315 assert!(aggregate.fpcs[0].disable_fpc);
316
317 let padi = &pppoe.packet_types[1];
318 assert_eq!(padi.name, "padi");
319 assert!(padi.bypass_aggregate);
320 assert!(padi.disable_logging);
321 assert_eq!(padi.priority.as_deref(), Some("medium"));
322 assert_eq!(padi.hostbound_queue.as_deref(), Some("3"));
323 assert_eq!(padi.flow_detection_mode.as_deref(), Some("on"));
324 assert_eq!(padi.flow_detect_time.as_deref(), Some("5"));
325 assert_eq!(padi.flow_recover_time.as_deref(), Some("90"));
326 assert_eq!(padi.flow_timeout_time.as_deref(), Some("600"));
327 assert!(padi.no_flow_logging);
328 assert!(padi.timeout_active_flows);
329 let bandwidth = padi.flow_level_bandwidth.as_ref().unwrap();
330 assert_eq!(bandwidth.logical_interface.as_deref(), Some("100"));
331 assert_eq!(bandwidth.subscriber.as_deref(), Some("10"));
332 assert_eq!(bandwidth.physical_interface, None);
333 assert_eq!(
334 padi.flow_level_control.as_ref().unwrap().physical_interface.as_deref(),
335 Some("drop")
336 );
337 assert_eq!(
338 padi.flow_level_detection.as_ref().unwrap().subscriber.as_deref(),
339 Some("off")
340 );
341
342 assert_eq!(ddos.protocols[1].packet_types[0].range.as_deref(), Some("5000"));
343 }
344
345 #[test]
346 fn reads_flow_detection_written_as_a_block() {
347 let ddos = system(&juniper::parse_braces(
348 r#"
349system {
350 ddos-protection {
351 protocols {
352 dhcpv4 {
353 discover {
354 flow-detection {
355 flow-detect-time 4;
356 no-flow-logging;
357 flow-level-bandwidth {
358 subscriber 20;
359 }
360 }
361 }
362 }
363 }
364 }
365}
366"#,
367 ))
368 .unwrap()
369 .ddos_protection
370 .unwrap();
371 let discover = &ddos.protocols[0].packet_types[0];
372 assert_eq!(discover.flow_detect_time.as_deref(), Some("4"));
373 assert!(discover.no_flow_logging);
374 assert_eq!(
375 discover.flow_level_bandwidth.as_ref().unwrap().subscriber.as_deref(),
376 Some("20")
377 );
378 assert_eq!(discover.bandwidth, None);
379 }
380
381 #[test]
382 fn no_ddos_protection_is_none() {
383 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
384 assert!(system.ddos_protection.is_none());
385 }
386}