Handle non echo reply packets properly for ICMPv6 Packets

This commit is contained in:
Jeremy Wall 2021-02-02 20:49:29 -05:00
parent 060e505ab0
commit 53b9948491

View File

@ -25,7 +25,7 @@ use icmp_socket::{
packet::{Icmpv4Message, Icmpv6Message, WithEchoRequest}, packet::{Icmpv4Message, Icmpv6Message, WithEchoRequest},
IcmpSocket, IcmpSocket4, IcmpSocket6, Icmpv4Packet, Icmpv6Packet, IcmpSocket, IcmpSocket4, IcmpSocket6, Icmpv4Packet, Icmpv6Packet,
}; };
use log::{debug, error, info}; use log::{error, info};
use prometheus::{CounterVec, GaugeVec}; use prometheus::{CounterVec, GaugeVec};
use socket2::{self, SockAddr}; use socket2::{self, SockAddr};
@ -206,7 +206,7 @@ pub fn start_echo_loop(
return None; return None;
} }
if sequence != seq { if sequence != seq {
info!( error!(
"ICMP: Discarding sequence {}, expected sequence {}", "ICMP: Discarding sequence {}, expected sequence {}",
sequence, seq sequence, seq
); );
@ -230,6 +230,7 @@ pub fn start_echo_loop(
p => { p => {
// We ignore the rest. // We ignore the rest.
info!("ICMP Unhandled packet {:?}", p); info!("ICMP Unhandled packet {:?}", p);
return None;
} }
} }
Some(()) Some(())
@ -247,19 +248,69 @@ pub fn start_echo_loop(
match p.message { match p.message {
Icmpv6Message::Unreachable { Icmpv6Message::Unreachable {
_unused, _unused,
invoking_packet: _, invoking_packet,
} => { } => {
ping_counter match Icmpv6Packet::parse(&invoking_packet) {
.with(&prometheus::labels! {"result" => "unreachable", "domain" => domain_name}) Ok(Icmpv6Packet {
.inc(); typ: _,
code: _,
checksum: _,
message:
Icmpv6Message::EchoRequest {
identifier,
sequence: _,
payload: _,
},
}) => {
if identifier == 42 {
ping_counter
.with(&prometheus::labels! {"result" => "unreachable", "domain" => domain_name})
.inc();
return Some(());
}
}
Err(e) => {
// We ignore these as well but log it.
error!("ICMP: Error parsing Unreachable invoking packet {:?}", e);
}
_ => {
// We ignore these
}
};
return None;
} }
Icmpv6Message::ParameterProblem { Icmpv6Message::ParameterProblem {
pointer: _, pointer: _,
invoking_packet: _, invoking_packet,
} => { } => {
ping_counter match Icmpv6Packet::parse(&invoking_packet) {
.with(&prometheus::labels! {"result" => "parameter_problem", "domain" => domain_name}) Ok(Icmpv6Packet {
.inc(); typ: _,
code: _,
checksum: _,
message:
Icmpv6Message::EchoRequest {
identifier,
sequence: _,
payload: _,
},
}) => {
if identifier == 42 {
ping_counter
.with(&prometheus::labels! {"result" => "parameter_problem", "domain" => domain_name})
.inc();
return Some(());
}
}
Err(e) => {
// We ignore these as well but log it.
error!("ICMP: Error parsing Unreachable invoking packet {:?}", e);
}
_ => {
// We ignore these
}
}
return None;
} }
Icmpv6Message::EchoReply { Icmpv6Message::EchoReply {
identifier, identifier,
@ -271,7 +322,7 @@ pub fn start_echo_loop(
return None; return None;
} }
if sequence != seq { if sequence != seq {
info!("ICMP: Discarding sequence {}", sequence); error!("ICMP: Discarding sequence {}", sequence);
return None; return None;
} }
let elapsed = let elapsed =
@ -295,6 +346,7 @@ pub fn start_echo_loop(
} }
_ => { _ => {
// We ignore the rest. // We ignore the rest.
return None;
} }
} }
Some(()) Some(())