This page explains how to set up a simple stateful firewall using iptables. It also tries to explain what the rules mean and why they are needed. For simplicity, this HOWTO is split into two parts. The first part deals with a single machine, the second part sets up a NAT gateway.

BIG FAT WARNING: For simplicity, the rules are given in the order that they are executed later. If you are logged into a remote machine, following the rules in the exact order they are given MAY result in your machine being unavailable to the network. Only do the steps below while you are logged in locally unless you know what you are doing. You have been warned.

Setting up a single machine

Creating necessary chains

For this basic setup, we will create two custom chains. Their meaning is explained later:

# iptables -N open
# iptables -N interfaces

The INPUT chain

Every packet that is received by any network interface and has one of the local host's IP addresses in the destination header will pass the INPUT chain first. In this chain we make sure that only the packets that we want are accepted.

The first rule will accept all ICMP messages. ICMP means Internet Control Message Protocol. Some ICMP messages are very important, some are less important (like echo requests (pings)). None of them hurts, so it is generally a good idea not to block them:

# iptables -A INPUT -p icmp -j DROP

The next rule will make sure that none of the traffic that belongs to already established connections will be dropped. This can be done with the state match. A package can have one of the four states ESTABLISHED, RELATED, NEW and INVALID. We want to accept all packets that are in state ESTABLISHED or RELATED, hence the name "stateful firewall":

# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

In most cases, we do not want to deny all incoming connections. That's why we set up the two custom chains open and interfaces. For now, we are adding a rule for each of them:

# iptables -A INPUT -j interfaces
# iptables -A INPUT -j open

Now, with the last two rules, we drop everything that hasn't been explicitly accepted above. For TCP packets, we deny the connection with a tcp-reset. Incoming UDP packets are answered with an ICMP message. This way, we imitate Linux's default behaviour:

All other protocols than TCP, UDP and ICMP are dropped (unless they matched the state match above). We do this by setting the policy for the INPUT chain to DROP

# iptables -P INPUT DROP

The FORWARD chain

If you want to set up your machine as a NAT gateway, please look at the second section of this HOWTO. For a single machine, we simply set the policy of the FORWARD chain to DROP and move on:

# iptables -P FORWARD DROP

The OUTPUT chain

We have no intention of filtering any outgoing traffic. This would make the setup much more complicated and requires some extra thought. In this simple case, we set the OUTPUT policy to ACCEPT.

# iptables -P OUTPUT ACCEPT

The interfaces chain

We use the interfaces chain to accept any traffic from trusted interfaces. The first rule is absolutely necessary:

# iptables -A interfaces -i lo -j ACCEPT

This accepts every traffic from the loopback interface, which is necessary for many applications to work properly. You can add more interfaces here. For example, if you want to accept all incoming traffic from the interface eth0, add this rule:

# iptables -A interfaces -i eth0 -j ACCEPT

Incoming connections on other interfaces will be denied, unless they hit another exception in the open chain.

The open chain

The open chain contains rules for accepting incoming connections on specific ports or protocols. For example, if you want to accept ssh connections on every interface, add this rule:

# iptables -A open -p tcp --dport 22 -j ACCEPT

To accept incoming HTTP connections on the interface ppp0:

# iptables -A open -i ppp0 -p tcp --dport 80 -j ACCEPT

To accept all incoming tcp connections with destination ports 65000 to 65005 on interface foo:

# iptables -A open -i foo -p tcp --dport 65000:65005 -j ACCEPT

The same is of course possible with udp:

# iptables -A open -i foo -p udp --dport 65000:65005 -j ACCEPT

or with other protocols than tcp and udp:

# iptables -A open -i foo -p 123 -j ACCEPT

See man iptables for more advanced rules, like matching multiple ports or protocols.

Saving the rules

Now, the rules are ready and should be saved to your hard drive. First, we edit the configuration file /etc/conf.d/iptables:

and make sure your rules are loaded when you boot by editing /etc/rc.conf:

# DAEMONS=(... iptables ...)

Setting up a NAT gateway

This section of the HOWTO deals with NAT gateways. It is assumed that you already read the first part of the HOWTO and set up the INPUT, OUTPUT, open and interfaces chains like described above. All rules so far have been created in the filter table. In this section, we will also have to use the nat table.

Setting up the filter table

Before we start, make sure that the FORWARD chain is empty:

# iptables -F FORWARD

Creating necessary chains

In our setup, we will use another two chains in the filter table, the fw-interfaces and fw-open chains. Create them with the commands

# iptables -N fw-interfaces
# iptables -N fw-open

Setting up the FORWARD chain

Setting up the FORWARD chain is similar to the INPUT chain in the first section.

Some networks and servers block ICMP messages and are preventing path MTU detection. If your outgoing interface's MTU is lower than the MTU on the local network (like with PPPoE connections), this may prevent you from communicating with such servers. The first rule works around this problem, at least for TCP connections:

Setting up the fw-interfaces and fw-open chains

The meaning of the fw-interfaces and fw-open chains is explained later, when we deal with the POSTROUTING and PREROUTING chains in the nat table, respectively.

Setting up the nat table

All over this section, we assume that the outgoing interface (the one with the public internet IP) is ppp0. Keep in mind that you have to change the name in all following rules if your outgoing interface has another name.

Setting up the POSTROUTING chain

Now, we have to define who is allowed to connect to the internet. Let's assume we have the subnet 192.168.0.0/255.255.255.0 (which means all addresses that are of the form 192.168.0.*) on eth0. We first need to accept the machines on this interface in the FORWARD table, that's why we created the fw-interfaces chain above:

# iptables -A fw-interfaces -i eth0 -j ACCEPT

Now, we have to alter all outgoing packets so that they have our public IP address as the source address, instead of the local LAN address. To do this, we use the MASQUERADE target:

The last step is to enable IP Forwarding (if it isn't already enabled):

# echo 1 > /proc/sys/net/ipv4/ip_forward

Machines from these subnets can now use your new NAT machine as their gateway. Note that you may want to set up a DNS and DHCP server like dnsmasq or a combination of bind and dhcpd to simplify network settings DNS resolution on the client machines. This is not the topic of this HOWTO.

Setting up the PREROUTING chain

Sometimes, we want to forward an incoming connection from the gateway to a LAN machine. To do this, we use the fw-open chain defined above, as well as the PREROUTING chain in the nat table

I will give two simple examples: First, we want to forward all incoming SSH connections (port 22) to the ssh server in the machine 192.168.0.5: