My problem needs a introdcution.
I installed home assistant on raspberry pi 4.
On my NAS i installed a ubuntu docker with portainer.
I created a cloudflared tunnel to https://hassio.domainname.com.
All was working perfectly.
Matthew Hodgkins pointed me on security in his blog Securing Home Assistant with Cloudflare
To add security want to restrict the incomming (allowed) ip-ddresses. I needed another solution because i installed cloudflare on my NAS.
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
This line is an iptables rule that allows incoming network traffic that is related to or associated with an already established connection. Here is a breakdown of the individual components of the command:
iptables: This command is used to manage firewall rules in Linux.
-A INPUT: This option appends a new rule to the INPUT chain, which is responsible for handling incoming network traffic.
-m conntrack: This option specifies the module to use for connection tracking.
--ctstate RELATED,ESTABLISHED: This option matches packets that are related to or part of an established connection. This includes traffic that is associated with an existing connection such as responses to requests sent by the host or traffic related to the stateful protocol.
-j ACCEPT: This option specifies what action to take when a packet matches the rule, in this case, to accept the packet and allow it through the firewall.
iptables -A INPUT -i lo -j ACCEPT
This line is another iptables rule that allows incoming traffic on the loopback interface. Here’s a breakdown of the individual components of the command:
iptables: This command is used to manage firewall rules in Linux.
-A INPUT: This option appends a new rule to the INPUT chain, which is responsible for handling incoming network traffic.
-i lo: This option specifies the loopback interface, which is a virtual network interface that a computer uses to communicate with itself. Traffic on the loopback interface is not seen by other computers on the network.
-j ACCEPT: This option specifies what action to take when a packet matches the rule, in this case, to accept the packet and allow it through the firewall.
This rule is important because many services, such as databases and web servers, use the loopback interface to communicate with themselves. Allowing traffic on the loopback interface is necessary for these services to function properly.
But im still not sure what it does.
Can you try to explain why i should use it?
I do not understand “related to or associated with an already established connection” and “loopback interface”
Cloudflare offers cloudflared as a Docker container, this saves the hassle of having to manually install the tunnel service and streamlines the update process.
Note that cloudflared only initiates outbound connections to both Cloudflare and your local network, so I’m not sure what you’re trying to achieve by allowing connections from the public Cloudflare IP addresses in your INPUT chain.
This is an old post, but maybe someone will find this hint useful. You should change those instructions positions, as because of -A (append) adds them in this order to the end of iptables file, so the first one cuts all incoming packets:
iptables -A INPUT -j DROP
And the second one is never evaluated:
iptables -A INPUT -s 173.245.48.0/20 -j ACCEPT
When you change their order, when the above rule will be evaluated successfully with -j ACCEPT, then the -j DROP rule won’t be evaluated.