One of the most difficult problems when dealing with a firewall is that the network or subnetwork we want to protect usually has to be split into at least two subnetworks: one on the external side and one on the internal, protected side. This, apart from the planning stage, can result in the reconfiguration of all machines in the network to the new configuration. What is worse is that in case of a hardware fault of the firewall, you'll have to reconfigure all machines in your network so they will be able to see the outside until you repair the firewall machine. The configuration of the firewall can be even harder if you don't have access to the configuration of the machine that actually connects your network to the external world, very often a router or something leased from a telco (telephone company).
We are going to explain a smarter way of adding a firewall to your network without breaking it into subnetworks or reconfiguring any machine on the internal or external network, except from the firewall machine itself, by just fooling the other machines into thinking nothing changed. We will cover the aspects of the network configuration and packet routing, not real packet-filtering firewalling, since this has already been covered in depth in another Linux Journal issue (see Resources).
In the practical examples, we will pretend we have a C class network, where our contact with the external network is the .1 machine in the network (let it be a router, a machine or whatever). We will assume the IP numbers .2 and .3 to be free for our use. We will also need another IP greater than or equal to .4, so we must be sure these aren't used for other purposes. Of course, with a different network configuration, you'll have to adjust some calculations to make the method fit your needs. To make things even clearer in the examples below, let's fix our C class network to the 22.214.171.124 one.
As for the Linux box that will work as a firewall, you must actually have the kernel compiled with the usual networking options, including IP forwarding and IP firewalling and whatever you else may need, for example, IP accounting. Also, enable IP forwarding in the kernel; in newer kernels, this is done by enabling it directly in the /proc file-system. (For example, by using echo 1>/proc/sys/net/ipv4/ip_forward. Your initialization scripts should take care of this.) Another thing to note is that you should have a working arp program installed on your system. In fact, some distributions are shipped with an arp binary that is compiled without the new SIOCSARP kernel interface, so it doesn't work, and if you forget to check this one thing, it may cause you many unnecessary headaches.
The Linux box must have two Ethernet cards installed: eth0, connected to the internal network; and eth1, directly connected to the gateway machine for our network to the world.
Now, the network interfaces on the firewall machine must be configured. The internal network interface will be configured in the same way as the other internal machines, as if it had all machines on the network. So in our class C example, the eth0 configuration would be the following:
inet addr : 126.96.36.199 network : 188.8.131.52 broadcast : 184.108.40.206 netmask : 255.255.255.0
Remember that in place of 220.127.116.11, you can use any other unused address. The commands to do this are
ifconfig eth0 18.104.22.168 netmask 255.255.255.0\ broadcast 22.214.171.124 route add -net 126.96.36.199This configuration will also apply to all machines in the internal network behind the firewall; of course, the IP address will change on each.
The second network interface, eth1, will be configured as if it had a very small network; actually, four IPs is the least we can manage. This is where the firewall machine and the gateway will be placed.
inet addr : 188.8.131.52 network : 184.108.40.206 broadcast : 220.127.116.11 netmask : 255.255.255.252
This is obtained using the following commands:
ifconfig eth1 18.104.22.168 netmask 255.255.255.252\ broadcast 22.214.171.124 route add -net 126.96.36.199 route add default gw 188.8.131.52The routing table is now set, so that packets for the entire class C network will be redirected to the internal eth0 interface, while the packets for the small network will be routed to the external eth1 interface. Finally, the default gateway has to be set for all machines on the internal network, that is, 184.108.40.206. Please note that this routing scheme works because the most specific route is applied first.
There are two more questions to solve. First, how will the gateway know how to reach the internal machines? Remember, we haven't changed the gateway configuration, so it still thinks it has the C class network attached to it. Second, how will the machines be able to reach the gateway? After all, they still believe they have the entire C class network, including 220.127.116.11, on their network interface. Well, it will be easy; we just make believe all the machines are on a class C network. The trick is to hack the process of translating the IP address to the hardware (in our case, Ethernet) address, which is called the ARP (address resolution protocol). (If you're not familiar with this, please consult the NET-HOWTO and Proxy-ARP miniHOWTO.) This can easily be done by telling our firewall machine to answer all ARP requests for the gateway on the internal network and reply to all requests for any internal machines from the gateway. Practically, this is done in two stages. First, by publishing, via arp, the gateway and the firewall machine on the internal network, more exactly:
arp -v -i eth0 -Ds 18.104.22.168 eth0 pub arp -v -i eth0 -Ds 22.214.171.124 eth0 pub
Thus, when someone asks for .1 or .2 on the internal network, the firewall will reply, giving its Ethernet hardware address. In the second stage, we will publish all internal network IPs from .4 to .255, on the firewall-gateway small network. For the entire C class network, it look like this:
arp -v -i eth1 -Ds 126.96.36.199 eth1 netmask\ 255.255.255.128 pub arp -v -i eth1 -Ds 188.8.131.52 eth1 netmask\ 255.255.255.192 pub arp -v -i eth1 -Ds 184.108.40.206 eth1 netmask\ 255.255.255.224 pub arp -v -i eth1 -Ds 220.127.116.11 eth1 netmask\ 255.255.255.240 pub arp -v -i eth1 -Ds 18.104.22.168 eth1 netmask\ 255.255.255.248 pub arp -v -i eth1 -Ds 22.214.171.124 eth1 netmask\ 255.255.255.252 pubThis way, we have partitioned the address space and published all our IPs. When the gateway asks for the hardware address of an internal machine, the firewall will reply giving its address. Since we turned on the IP packet forwarding, once the firewall has a packet and replies to the ARP request, it will forward it to the destination machine according to the routing table.
This solution has proven to be very useful for us, especially when we had to enhance the existing network without causing “too much trouble”. The last step is, of course, tuning the firewall, IP accounting, transparent proxy or whatever you need on the Linux box—but that is another story.
Federico is studying computer science at the University of Udine. When not hacking or coding he enjoys reading sf, listening to music and playing guitar. He can be reached at email@example.com.
Christian Pellegrin is studying astrophysics at the University of Trieste and works part-time as a system administrator and teacher in a high school. When not playing with Linux and other fun software or hardware he enjoys discussing who is the best film director of all times with his girlfriend. E-mails are welcome at firstname.lastname@example.org.
- One Port to Rule Them All!
- Privacy Is Personal
- PHP for Non-Developers
- Secure Server Deployments in Hostile Territory
- Linux Kernel 4.1 Released
- Django Templates
- July 2015 Issue of Linux Journal: Mobile
- A Code Boot Camp for Underprivileged Kids
- Practical Books for the Most Technical People on the Planet
- The AtoMiC Toolkit!