Handle Unreachables correctnly

This commit is contained in:
Jeremy Wall 2021-01-31 08:58:27 -05:00
parent 57ff47b36f
commit 93faffbd42

View File

@ -143,19 +143,11 @@ pub fn start_echo_loop(
let mut socket = IcmpSocket4::try_from(Ipv4Addr::new(0, 0, 0, 0)).unwrap(); let mut socket = IcmpSocket4::try_from(Ipv4Addr::new(0, 0, 0, 0)).unwrap();
socket.set_max_hops(MAXHOPS.flag as u32); socket.set_max_hops(MAXHOPS.flag as u32);
let packet_handler = |p: Icmpv4Packet, let packet_handler = |p: Icmpv4Packet,
s: SockAddr, _s: SockAddr,
send_time: Instant, send_time: Instant,
seq: u16| seq: u16|
-> Option<()> { -> Option<()> {
// We only want to handle replies for the address we are pinging. // We only want to handle replies for the address we are pinging.
if let Some(addr) = s.as_inet() {
if &dest != addr.ip() {
info!("ICMP: Packet for wrong address: {}", addr.ip());
return None;
}
} else {
return None;
};
match p.message { match p.message {
Icmpv4Message::ParameterProblem { Icmpv4Message::ParameterProblem {
pointer: _, pointer: _,
@ -170,16 +162,14 @@ pub fn start_echo_loop(
padding: _, padding: _,
header: _, header: _,
} => { } => {
// // If we got unreachable we need to set up a new sender. info!(
// error!("{:?}", r); "ICMP: Destination Unreachable {} from {}",
// info!("Restarting our sender"); dest,
info!("ICMP: Destination Unreachable {}", dest); _s.as_inet().unwrap().ip()
);
ping_counter ping_counter
.with(&prometheus::labels! {"result" => "unreachable", "domain" => domain_name}) .with(&prometheus::labels! {"result" => "unreachable", "domain" => domain_name})
.inc(); .inc();
// let resolved = resolve_host_address(domain_name);
// let mut new_sender = Ekko::with_target(&resolved).unwrap();
// std::mem::swap(&mut sender, &mut new_sender);
} }
Icmpv4Message::TimeExceeded { Icmpv4Message::TimeExceeded {
padding: _, padding: _,
@ -191,13 +181,17 @@ pub fn start_echo_loop(
.inc(); .inc();
} }
Icmpv4Message::EchoReply { Icmpv4Message::EchoReply {
identifier: _, identifier,
sequence, sequence,
payload: _, payload: _,
} => { } => {
if identifier != 42 {
info!("ICMP: Discarding wrong identifier {}", identifier);
return None;
}
if sequence != seq { if sequence != seq {
info!("ICMP: Discarding sequence {}", sequence); info!("ICMP: Discarding sequence {}", sequence);
return Some(()); return None;
} }
let elapsed = let elapsed =
Instant::now().sub(send_time.clone()).as_micros() as f64 / 1000.00; Instant::now().sub(send_time.clone()).as_micros() as f64 / 1000.00;
@ -227,18 +221,10 @@ pub fn start_echo_loop(
let mut socket = IcmpSocket6::try_from(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).unwrap(); let mut socket = IcmpSocket6::try_from(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).unwrap();
socket.set_max_hops(MAXHOPS.flag as u32); socket.set_max_hops(MAXHOPS.flag as u32);
let packet_handler = |p: Icmpv6Packet, let packet_handler = |p: Icmpv6Packet,
s: SockAddr, _s: SockAddr,
send_time: Instant, send_time: Instant,
seq: u16| seq: u16|
-> Option<()> { -> Option<()> {
// We only want to handle replies for the address we are pinging.
if let Some(addr) = s.as_inet6() {
if &dest != addr.ip() {
return None;
}
} else {
return None;
};
match p.message { match p.message {
Icmpv6Message::Unreachable { Icmpv6Message::Unreachable {
_unused, _unused,
@ -257,13 +243,17 @@ pub fn start_echo_loop(
.inc(); .inc();
} }
Icmpv6Message::EchoReply { Icmpv6Message::EchoReply {
identifier: _, identifier,
sequence, sequence,
payload: _, payload: _,
} => { } => {
if identifier != 42 {
info!("ICMP: Discarding wrong identifier {}", identifier);
return None;
}
if sequence != seq { if sequence != seq {
info!("ICMP: Discarding sequence {}", sequence); info!("ICMP: Discarding sequence {}", sequence);
return Some(()); return None;
} }
let elapsed = let elapsed =
Instant::now().sub(send_time.clone()).as_micros() as f64 / 1000.00; Instant::now().sub(send_time.clone()).as_micros() as f64 / 1000.00;