I have a server hosted by hetzner with a single public ip address which runs proxmox and some VMs. This ip address is configured inside /etc/interfaces like this:
auto enp35s0
iface enp35s0 inet static
address {{my-public-ip}}/{{subnet}}
gateway {{hetzner-gateway}}
up route add -net {{hetzner-ip}} netmask 255.255.255.192 gw {{hetzner-gateway}} dev enp35s0
This configuration was done by hetzner.
Because I don't want to get additional ip addresses from hetzner I masquerade that ip for an internal VM-Network:
auto vmbr0
iface vmbr0 inet static
address 172.16.0.1/24
bridge-ports none
bridge-stp off
bridge-fd 0
post-up echo 1 > /proc/sys/net/ipv4/ip_forward
post-up iptables -t nat -A POSTROUTING -s '172.16.0.0/24' -o enp35s0 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -s '172.16.0.0/24' -o enp35s0 -j MASQUERADE
With this my VMs have internet access and can reach each other.
Because iptables port forwarding is a bit too complicated for me I've started using firewalld. In there I have my enp35s0 interface assigned to the external zone and vmbr0 to trusted. I know maybe I should assign it to internal instead but currently it doesn't really make a difference (or I think so in my problem case).
I now have a service running inside a VM with the ip 172.16.0.3 on port 38080. To reach this service I add a port forwarding rule in firewalld: port=38080:proto=tcp:toport=38080:toaddr=172.16.0.3
. With that I can reach that service from outside of this server machine.
The problem now is, that if I use a software like uptime-kuma and run it also inside a VM on the same physical machine, I can't reach that service on port 38080 because the port forwarding is only done for external requests. Important here is, that the hostname that uptime-kuma uses is the FQDN that gets resolved to the public ip address of my host machine.
So to make this possible I add the same port forwarding rule to the trusted zone of firewalld because my vmbr0 interface is in there and from that interface comes the request.
Now this connection does work and my software (uptime-kuma) can reach my service.
The large problem now is, that EVERY request from inside the Virtual network which wants to use the port 38080 gets redirected to that VM (172.16.0.3), even those that go to a completely different server.
How can I tell firewalld to only redirect that traffic if the request was actually targeted at the host machine?