2024-02-09 13:06:00 +00:00
|
|
|
// Referenced https://github.com/vi/wgslirpy/blob/master/crates/libwgslirpy/src/router.rs as a very interesting way to implement NAT.
|
|
|
|
// hypha will heavily change how the original code functions however. NatKey was a very useful example of what we need to store in a NAT map.
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use async_trait::async_trait;
|
2024-02-10 12:00:15 +00:00
|
|
|
use etherparse::Ethernet2Slice;
|
2024-02-09 13:06:00 +00:00
|
|
|
use etherparse::IpNumber;
|
|
|
|
use etherparse::IpPayloadSlice;
|
|
|
|
use etherparse::Ipv4Slice;
|
|
|
|
use etherparse::LinkSlice;
|
|
|
|
use etherparse::NetSlice;
|
|
|
|
use etherparse::SlicedPacket;
|
|
|
|
use etherparse::TcpHeaderSlice;
|
|
|
|
use etherparse::UdpHeaderSlice;
|
|
|
|
use smoltcp::wire::EthernetAddress;
|
|
|
|
use smoltcp::wire::IpAddress;
|
|
|
|
use smoltcp::wire::IpEndpoint;
|
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fmt::Display;
|
2024-02-10 12:00:15 +00:00
|
|
|
use tokio::sync::mpsc::Sender;
|
2024-02-09 13:06:00 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
|
2024-02-10 12:00:15 +00:00
|
|
|
pub enum NatKeyProtocol {
|
|
|
|
Tcp,
|
|
|
|
Udp,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct NatKey {
|
|
|
|
pub protocol: NatKeyProtocol,
|
|
|
|
pub client_mac: EthernetAddress,
|
|
|
|
pub local_mac: EthernetAddress,
|
|
|
|
pub client_ip: IpEndpoint,
|
|
|
|
pub external_ip: IpEndpoint,
|
2024-02-09 13:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for NatKey {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-02-10 12:00:15 +00:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{} -> {} {:?} {} -> {}",
|
|
|
|
self.client_mac, self.local_mac, self.protocol, self.client_ip, self.external_ip
|
|
|
|
)
|
2024-02-09 13:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait NatHandler: Send {
|
|
|
|
async fn receive(&self, packet: &[u8]) -> Result<()>;
|
|
|
|
}
|
|
|
|
|
2024-02-10 12:00:15 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait NatHandlerFactory: Send {
|
|
|
|
async fn nat(&self, key: NatKey, sender: Sender<Vec<u8>>) -> Option<Box<dyn NatHandler>>;
|
|
|
|
}
|
|
|
|
|
2024-02-09 13:06:00 +00:00
|
|
|
pub struct NatTable {
|
|
|
|
inner: HashMap<NatKey, Box<dyn NatHandler>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NatTable {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
inner: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NatRouter {
|
2024-02-10 12:00:15 +00:00
|
|
|
_local_mac: EthernetAddress,
|
2024-02-09 13:06:00 +00:00
|
|
|
factory: Box<dyn NatHandlerFactory>,
|
|
|
|
table: NatTable,
|
2024-02-10 12:00:15 +00:00
|
|
|
tx_sender: Sender<Vec<u8>>,
|
2024-02-09 13:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NatRouter {
|
2024-02-10 12:00:15 +00:00
|
|
|
pub fn new(
|
|
|
|
factory: Box<dyn NatHandlerFactory>,
|
|
|
|
mac: EthernetAddress,
|
|
|
|
tx_sender: Sender<Vec<u8>>,
|
|
|
|
) -> Self {
|
2024-02-09 13:06:00 +00:00
|
|
|
Self {
|
2024-02-10 12:00:15 +00:00
|
|
|
_local_mac: mac,
|
2024-02-09 13:06:00 +00:00
|
|
|
factory,
|
|
|
|
table: NatTable::new(),
|
2024-02-10 12:00:15 +00:00
|
|
|
tx_sender,
|
2024-02-09 13:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn process(&mut self, data: &[u8]) -> Result<()> {
|
|
|
|
let packet = SlicedPacket::from_ethernet(data)?;
|
|
|
|
let Some(ref link) = packet.link else {
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
|
|
|
let LinkSlice::Ethernet2(ref ether) = link else {
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
|
|
|
let _mac = EthernetAddress(ether.destination());
|
|
|
|
|
|
|
|
let Some(ref net) = packet.net else {
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
|
|
|
match net {
|
|
|
|
NetSlice::Ipv4(ipv4) => {
|
2024-02-10 12:00:15 +00:00
|
|
|
self.process_ipv4(data, ether, ipv4).await?;
|
2024-02-09 13:06:00 +00:00
|
|
|
}
|
2024-02-10 12:00:15 +00:00
|
|
|
|
2024-02-09 13:06:00 +00:00
|
|
|
_ => {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-10 12:00:15 +00:00
|
|
|
pub async fn process_ipv4<'a>(
|
|
|
|
&mut self,
|
|
|
|
data: &[u8],
|
|
|
|
ether: &Ethernet2Slice<'a>,
|
|
|
|
ipv4: &Ipv4Slice<'a>,
|
|
|
|
) -> Result<()> {
|
2024-02-09 13:06:00 +00:00
|
|
|
let source_addr = IpAddress::Ipv4(ipv4.header().source_addr().into());
|
|
|
|
let dest_addr = IpAddress::Ipv4(ipv4.header().destination_addr().into());
|
|
|
|
match ipv4.header().protocol() {
|
|
|
|
IpNumber::TCP => {
|
2024-02-10 12:00:15 +00:00
|
|
|
self.process_tcp(data, ether, source_addr, dest_addr, ipv4.payload())
|
2024-02-09 13:06:00 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
IpNumber::UDP => {
|
2024-02-10 12:00:15 +00:00
|
|
|
self.process_udp(data, ether, source_addr, dest_addr, ipv4.payload())
|
2024-02-09 13:06:00 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn process_tcp<'a>(
|
|
|
|
&mut self,
|
|
|
|
data: &'a [u8],
|
2024-02-10 12:00:15 +00:00
|
|
|
ether: &Ethernet2Slice<'a>,
|
2024-02-09 13:06:00 +00:00
|
|
|
source_addr: IpAddress,
|
|
|
|
dest_addr: IpAddress,
|
|
|
|
payload: &IpPayloadSlice<'a>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let header = TcpHeaderSlice::from_slice(payload.payload)?;
|
|
|
|
let source = IpEndpoint::new(source_addr, header.source_port());
|
|
|
|
let dest = IpEndpoint::new(dest_addr, header.destination_port());
|
2024-02-10 12:00:15 +00:00
|
|
|
let key = NatKey {
|
|
|
|
protocol: NatKeyProtocol::Tcp,
|
|
|
|
client_mac: EthernetAddress(ether.destination()),
|
|
|
|
local_mac: EthernetAddress(ether.source()),
|
|
|
|
client_ip: source,
|
|
|
|
external_ip: dest,
|
2024-02-09 13:06:00 +00:00
|
|
|
};
|
|
|
|
self.process_nat(data, key).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn process_udp<'a>(
|
|
|
|
&mut self,
|
|
|
|
data: &'a [u8],
|
2024-02-10 12:00:15 +00:00
|
|
|
ether: &Ethernet2Slice<'a>,
|
2024-02-09 13:06:00 +00:00
|
|
|
source_addr: IpAddress,
|
|
|
|
dest_addr: IpAddress,
|
|
|
|
payload: &IpPayloadSlice<'a>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let header = UdpHeaderSlice::from_slice(payload.payload)?;
|
|
|
|
let source = IpEndpoint::new(source_addr, header.source_port());
|
|
|
|
let dest = IpEndpoint::new(dest_addr, header.destination_port());
|
2024-02-10 12:00:15 +00:00
|
|
|
let key = NatKey {
|
|
|
|
protocol: NatKeyProtocol::Udp,
|
|
|
|
client_mac: EthernetAddress(ether.destination()),
|
|
|
|
local_mac: EthernetAddress(ether.source()),
|
|
|
|
client_ip: source,
|
|
|
|
external_ip: dest,
|
2024-02-09 13:06:00 +00:00
|
|
|
};
|
|
|
|
self.process_nat(data, key).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn process_nat(&mut self, data: &[u8], key: NatKey) -> Result<()> {
|
|
|
|
let handler: Option<&mut Box<dyn NatHandler>> = match self.table.inner.entry(key) {
|
|
|
|
Entry::Occupied(entry) => Some(entry.into_mut()),
|
|
|
|
Entry::Vacant(entry) => {
|
2024-02-10 12:00:15 +00:00
|
|
|
if let Some(handler) = self.factory.nat(key, self.tx_sender.clone()).await {
|
2024-02-09 13:06:00 +00:00
|
|
|
Some(entry.insert(handler))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(handler) = handler {
|
|
|
|
handler.receive(data).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|