network: implement icmp nat support

This commit is contained in:
Alex Zenla
2024-02-10 21:13:47 +00:00
parent 4f0e505e2b
commit efe425b346
10 changed files with 470 additions and 105 deletions

View File

@ -1,13 +1,11 @@
use std::time::Duration;
use std::{net::IpAddr, time::Duration};
use anyhow::{anyhow, Result};
use async_ping::{
icmp_client::Config,
icmp_packet::{Icmp, Icmpv4},
PingClient,
};
use async_trait::async_trait;
use etherparse::{Icmpv4Header, Icmpv4Type, IpNumber, PacketBuilder, SlicedPacket};
use etherparse::{
Icmpv4Header, Icmpv4Type, Icmpv6Header, Icmpv6Type, IpNumber, NetSlice, PacketBuilder,
SlicedPacket,
};
use log::{debug, warn};
use smoltcp::wire::IpAddress;
use tokio::{
@ -15,7 +13,10 @@ use tokio::{
sync::mpsc::{Receiver, Sender},
};
use crate::nat::{NatHandler, NatKey};
use crate::{
icmp::{IcmpClient, IcmpProtocol, IcmpReply},
nat::{NatHandler, NatKey},
};
const ICMP_PING_TIMEOUT_SECS: u64 = 20;
const ICMP_TIMEOUT_SECS: u64 = 30;
@ -49,15 +50,7 @@ impl ProxyIcmpHandler {
tx_sender: Sender<Vec<u8>>,
reclaim_sender: Sender<NatKey>,
) -> Result<()> {
let client = PingClient::<icmp_client::impl_tokio::Client>::new(Some(Config::new()), None)?;
{
let client = client.clone();
tokio::spawn(async move {
client.handle_v4_recv_from().await;
});
}
let client = IcmpClient::new(IcmpProtocol::Icmp4)?;
let key = self.key;
tokio::spawn(async move {
if let Err(error) =
@ -70,7 +63,7 @@ impl ProxyIcmpHandler {
}
async fn process(
client: PingClient<icmp_client::impl_tokio::Client>,
client: IcmpClient,
key: NatKey,
mut rx_receiver: Receiver<Vec<u8>>,
tx_sender: Sender<Vec<u8>>,
@ -94,61 +87,102 @@ impl ProxyIcmpHandler {
continue;
};
let Some(ip) = net.ip_payload_ref() else {
continue;
};
match net {
NetSlice::Ipv4(ipv4) => {
if ipv4.header().protocol() != IpNumber::ICMP {
continue;
}
if ip.ip_number != IpNumber::ICMP {
continue;
}
let (header, payload) =
Icmpv4Header::from_slice(ipv4.payload().payload)?;
if let Icmpv4Type::EchoRequest(echo) = header.icmp_type {
let IpAddr::V4(external_ipv4) = key.external_ip.addr.into() else {
continue;
};
let (header, payload) = Icmpv4Header::from_slice(ip.payload)?;
if let Icmpv4Type::EchoRequest(echo) = header.icmp_type {
let result = client
.ping(
key.external_ip.addr.into(),
Some(echo.id),
Some(echo.seq),
payload,
Duration::from_secs(ICMP_PING_TIMEOUT_SECS),
)
.await;
match result {
Ok((icmp, _)) => match icmp {
Icmp::V4(Icmpv4::EchoReply(reply)) => {
let packet =
PacketBuilder::ethernet2(key.local_mac.0, key.client_mac.0);
let packet = match (key.external_ip.addr, key.client_ip.addr) {
(
IpAddress::Ipv4(external_addr),
IpAddress::Ipv4(client_addr),
) => packet.ipv4(external_addr.0, client_addr.0, 20),
(
IpAddress::Ipv6(external_addr),
IpAddress::Ipv6(client_addr),
) => packet.ipv6(external_addr.0, client_addr.0, 20),
_ => {
return Err(anyhow!("IP endpoint mismatch"));
}
};
let packet = packet.icmpv4_echo_reply(
reply.identifier.0,
reply.sequence_number.0,
);
let mut buffer: Vec<u8> = Vec::new();
packet.write(&mut buffer, &reply.payload)?;
if let Err(error) = tx_sender.try_send(buffer) {
debug!("failed to transmit icmp packet: {}", error);
let Some(IcmpReply::Icmp4 {
header: _,
echo,
payload,
}) = client
.ping4(
external_ipv4,
echo.id,
echo.seq,
payload,
Duration::from_secs(ICMP_PING_TIMEOUT_SECS),
)
.await?
else {
continue;
};
let packet =
PacketBuilder::ethernet2(key.local_mac.0, key.client_mac.0);
let packet = match (key.external_ip.addr, key.client_ip.addr) {
(
IpAddress::Ipv4(external_addr),
IpAddress::Ipv4(client_addr),
) => packet.ipv4(external_addr.0, client_addr.0, 20),
_ => {
return Err(anyhow!("IP endpoint mismatch"));
}
};
let packet = packet.icmpv4_echo_reply(echo.id, echo.seq);
let mut buffer: Vec<u8> = Vec::new();
packet.write(&mut buffer, &payload)?;
if let Err(error) = tx_sender.try_send(buffer) {
debug!("failed to transmit icmp packet: {}", error);
}
}
}
Icmp::V4(Icmpv4::Other(_type, _code, _payload)) => {}
NetSlice::Ipv6(ipv6) => {
if ipv6.header().next_header() != IpNumber::ICMP {
continue;
}
_ => {}
},
let (header, payload) =
Icmpv6Header::from_slice(ipv6.payload().payload)?;
if let Icmpv6Type::EchoRequest(echo) = header.icmp_type {
let IpAddr::V6(external_ipv6) = key.external_ip.addr.into() else {
continue;
};
Err(error) => {
debug!("proxy for icmp failed to emulate ICMP ping: {}", error);
let Some(IcmpReply::Icmp6 {
header: _,
echo,
payload,
}) = client
.ping6(
external_ipv6,
echo.id,
echo.seq,
payload,
Duration::from_secs(ICMP_PING_TIMEOUT_SECS),
)
.await?
else {
continue;
};
let packet =
PacketBuilder::ethernet2(key.local_mac.0, key.client_mac.0);
let packet = match (key.external_ip.addr, key.client_ip.addr) {
(
IpAddress::Ipv6(external_addr),
IpAddress::Ipv6(client_addr),
) => packet.ipv6(external_addr.0, client_addr.0, 20),
_ => {
return Err(anyhow!("IP endpoint mismatch"));
}
};
let packet = packet.icmpv6_echo_reply(echo.id, echo.seq);
let mut buffer: Vec<u8> = Vec::new();
packet.write(&mut buffer, &payload)?;
if let Err(error) = tx_sender.try_send(buffer) {
debug!("failed to transmit icmp packet: {}", error);
}
}
}
}