use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{active_children, arg_of, child_block, has_leaf, named_children};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct DdosFlowLevels {
pub logical_interface: Option<String>,
pub physical_interface: Option<String>,
pub subscriber: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DdosFpc {
pub slot: String,
pub bandwidth_scale: Option<String>,
pub burst_scale: Option<String>,
pub disable_fpc: bool,
pub hostbound_queue: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DdosPacketType {
pub name: String,
pub bandwidth: Option<String>,
pub burst: Option<String>,
pub bypass_aggregate: bool,
pub disable_fpc: bool,
pub disable_logging: bool,
pub disable_routing_engine: bool,
pub fpcs: Vec<DdosFpc>,
pub hostbound_queue: Option<String>,
pub priority: Option<String>,
pub recover_time: Option<String>,
pub range: Option<String>,
pub flow_detection_mode: Option<String>,
pub flow_detect_time: Option<String>,
pub flow_recover_time: Option<String>,
pub flow_timeout_time: Option<String>,
pub flow_level_bandwidth: Option<DdosFlowLevels>,
pub flow_level_control: Option<DdosFlowLevels>,
pub flow_level_detection: Option<DdosFlowLevels>,
pub no_flow_logging: bool,
pub timeout_active_flows: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DdosProtocolGroup {
pub name: String,
pub packet_types: Vec<DdosPacketType>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DdosGlobal {
pub disable_fpc: bool,
pub disable_logging: bool,
pub disable_routing_engine: bool,
pub flow_detection: bool,
pub flow_detection_mode: Option<String>,
pub flow_level_control: Option<String>,
pub flow_report_rate: Option<String>,
pub violation_report_rate: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DdosProtection {
pub global: Option<DdosGlobal>,
pub protocols: Vec<DdosProtocolGroup>,
pub traceoptions: Option<Traceoptions>,
}
fn levels(parent: &ConfigLine, keyword: &str) -> Option<DdosFlowLevels> {
let block = child_block(parent, keyword)?;
let level = |name: &str| arg_of(block, name).map(str::to_string);
Some(DdosFlowLevels {
logical_interface: level("logical-interface"),
physical_interface: level("physical-interface"),
subscriber: level("subscriber"),
})
}
fn parse_packet_type(name: &str, packet: &ConfigLine) -> DdosPacketType {
let flow = child_block(packet, "flow-detection");
let flow_field = |keyword: &str| {
arg_of(packet, keyword)
.or_else(|| flow.and_then(|f| arg_of(f, keyword)))
.map(str::to_string)
};
let flow_levels = |keyword: &str| levels(packet, keyword).or_else(|| flow.and_then(|f| levels(f, keyword)));
let flow_flag = |keyword: &str| has_leaf(packet, keyword) || flow.is_some_and(|f| has_leaf(f, keyword));
DdosPacketType {
name: name.to_string(),
bandwidth: arg_of(packet, "bandwidth").map(str::to_string),
burst: arg_of(packet, "burst").map(str::to_string),
bypass_aggregate: has_leaf(packet, "bypass-aggregate"),
disable_fpc: has_leaf(packet, "disable-fpc"),
disable_logging: has_leaf(packet, "disable-logging"),
disable_routing_engine: has_leaf(packet, "disable-routing-engine"),
fpcs: named_children(packet, "fpc")
.map(|(slot, fpc)| DdosFpc {
slot: slot.to_string(),
bandwidth_scale: arg_of(fpc, "bandwidth-scale").map(str::to_string),
burst_scale: arg_of(fpc, "burst-scale").map(str::to_string),
disable_fpc: has_leaf(fpc, "disable-fpc"),
hostbound_queue: arg_of(fpc, "hostbound-queue").map(str::to_string),
})
.collect(),
hostbound_queue: arg_of(packet, "hostbound-queue").map(str::to_string),
priority: arg_of(packet, "priority").map(str::to_string),
recover_time: arg_of(packet, "recover-time").map(str::to_string),
range: arg_of(packet, "range").map(str::to_string),
flow_detection_mode: flow_field("flow-detection-mode"),
flow_detect_time: flow_field("flow-detect-time"),
flow_recover_time: flow_field("flow-recover-time"),
flow_timeout_time: flow_field("flow-timeout-time"),
flow_level_bandwidth: flow_levels("flow-level-bandwidth"),
flow_level_control: flow_levels("flow-level-control"),
flow_level_detection: flow_levels("flow-level-detection"),
no_flow_logging: flow_flag("no-flow-logging"),
timeout_active_flows: flow_flag("timeout-active-flows"),
}
}
pub(super) fn parse_ddos_protection(ddos: &ConfigLine) -> DdosProtection {
DdosProtection {
global: child_block(ddos, "global").map(|global| DdosGlobal {
disable_fpc: has_leaf(global, "disable-fpc"),
disable_logging: has_leaf(global, "disable-logging"),
disable_routing_engine: has_leaf(global, "disable-routing-engine"),
flow_detection: has_leaf(global, "flow-detection"),
flow_detection_mode: arg_of(global, "flow-detection-mode").map(str::to_string),
flow_level_control: arg_of(global, "flow-level-control").map(str::to_string),
flow_report_rate: arg_of(global, "flow-report-rate").map(str::to_string),
violation_report_rate: arg_of(global, "violation-report-rate").map(str::to_string),
}),
protocols: child_block(ddos, "protocols")
.map(|protocols| {
active_children(protocols)
.map(|group| DdosProtocolGroup {
name: group.text.clone(),
packet_types: active_children(group)
.map(|packet| parse_packet_type(&packet.text, packet))
.collect(),
})
.collect()
})
.unwrap_or_default(),
traceoptions: parse_traceoptions(ddos),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_the_global_settings() {
let ddos = system(&juniper::parse_braces(
r#"
system {
ddos-protection {
global {
disable-fpc;
disable-logging;
disable-routing-engine;
flow-detection;
flow-detection-mode automatic;
flow-level-control police;
flow-report-rate 50;
violation-report-rate 200;
}
traceoptions {
file ddos.log;
flag all;
}
}
}
"#,
))
.unwrap()
.ddos_protection
.unwrap();
let global = ddos.global.unwrap();
assert!(global.disable_fpc);
assert!(global.disable_logging);
assert!(global.disable_routing_engine);
assert!(global.flow_detection);
assert_eq!(global.flow_detection_mode.as_deref(), Some("automatic"));
assert_eq!(global.flow_level_control.as_deref(), Some("police"));
assert_eq!(global.flow_report_rate.as_deref(), Some("50"));
assert_eq!(global.violation_report_rate.as_deref(), Some("200"));
assert_eq!(ddos.traceoptions.unwrap().file.as_deref(), Some("ddos.log"));
assert!(ddos.protocols.is_empty());
}
#[test]
fn extracts_protocol_groups_and_packet_types() {
let ddos = system(&juniper::parse_braces(
r#"
system {
ddos-protection {
protocols {
pppoe {
aggregate {
bandwidth 8000;
burst 2000;
recover-time 120;
fpc 2 {
bandwidth-scale 50;
burst-scale 25;
disable-fpc;
}
}
padi {
bandwidth 500;
bypass-aggregate;
disable-logging;
priority medium;
hostbound-queue 3;
flow-detection-mode on;
flow-detect-time 5;
flow-recover-time 90;
flow-timeout-time 600;
no-flow-logging;
timeout-active-flows;
flow-level-bandwidth {
logical-interface 100;
subscriber 10;
}
flow-level-control {
physical-interface drop;
}
flow-level-detection {
subscriber off;
}
}
}
sample {
aggregate {
range 5000;
}
}
}
}
}
"#,
))
.unwrap()
.ddos_protection
.unwrap();
assert_eq!(ddos.protocols.len(), 2);
let pppoe = &ddos.protocols[0];
assert_eq!(pppoe.name, "pppoe");
assert_eq!(pppoe.packet_types.len(), 2);
let aggregate = &pppoe.packet_types[0];
assert_eq!(aggregate.name, "aggregate");
assert_eq!(aggregate.bandwidth.as_deref(), Some("8000"));
assert_eq!(aggregate.burst.as_deref(), Some("2000"));
assert_eq!(aggregate.recover_time.as_deref(), Some("120"));
assert_eq!(aggregate.fpcs[0].slot, "2");
assert_eq!(aggregate.fpcs[0].bandwidth_scale.as_deref(), Some("50"));
assert_eq!(aggregate.fpcs[0].burst_scale.as_deref(), Some("25"));
assert!(aggregate.fpcs[0].disable_fpc);
let padi = &pppoe.packet_types[1];
assert_eq!(padi.name, "padi");
assert!(padi.bypass_aggregate);
assert!(padi.disable_logging);
assert_eq!(padi.priority.as_deref(), Some("medium"));
assert_eq!(padi.hostbound_queue.as_deref(), Some("3"));
assert_eq!(padi.flow_detection_mode.as_deref(), Some("on"));
assert_eq!(padi.flow_detect_time.as_deref(), Some("5"));
assert_eq!(padi.flow_recover_time.as_deref(), Some("90"));
assert_eq!(padi.flow_timeout_time.as_deref(), Some("600"));
assert!(padi.no_flow_logging);
assert!(padi.timeout_active_flows);
let bandwidth = padi.flow_level_bandwidth.as_ref().unwrap();
assert_eq!(bandwidth.logical_interface.as_deref(), Some("100"));
assert_eq!(bandwidth.subscriber.as_deref(), Some("10"));
assert_eq!(bandwidth.physical_interface, None);
assert_eq!(
padi.flow_level_control.as_ref().unwrap().physical_interface.as_deref(),
Some("drop")
);
assert_eq!(
padi.flow_level_detection.as_ref().unwrap().subscriber.as_deref(),
Some("off")
);
assert_eq!(ddos.protocols[1].packet_types[0].range.as_deref(), Some("5000"));
}
#[test]
fn reads_flow_detection_written_as_a_block() {
let ddos = system(&juniper::parse_braces(
r#"
system {
ddos-protection {
protocols {
dhcpv4 {
discover {
flow-detection {
flow-detect-time 4;
no-flow-logging;
flow-level-bandwidth {
subscriber 20;
}
}
}
}
}
}
}
"#,
))
.unwrap()
.ddos_protection
.unwrap();
let discover = &ddos.protocols[0].packet_types[0];
assert_eq!(discover.flow_detect_time.as_deref(), Some("4"));
assert!(discover.no_flow_logging);
assert_eq!(
discover.flow_level_bandwidth.as_ref().unwrap().subscriber.as_deref(),
Some("20")
);
assert_eq!(discover.bandwidth, None);
}
#[test]
fn no_ddos_protection_is_none() {
let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
assert!(system.ddos_protection.is_none());
}
}