Previous Next Contents

6. IP filtering setup (IPFWADM)

To start, you should have IP Forwarding turned on in your kernel and your system should be up and forwarding everything you send it. Your routing tables should be in place and you should be able to access everything, both from the inside out and from the outside in.

But, we're building a firewall so we need to start chocking down what everyone has access to.

In my system I created a couple of scripts to set the firewall forwarding policy and accounting policy. I call theses scripts from the /etc/rc.d scripts so my system is configured at boot time.

By default the IP Forwarding system in the Linux kernel forwards everything. Because of this, your firewall script should start by denying access to everything and flushing any ipfw rules in place from the last time it was run. This script will do the trick.

  # setup IP packet Accounting and Forwarding
  #   Forwarding
  # By default DENY all services
  ipfwadm -F -p deny
  # Flush all commands
  ipfwadm -F -f
  ipfwadm -I -f
  ipfwadm -O -f

Now we have the ultimate firewall. Nothing can get through. No doubt you have some services you need to forward so here are a few examples you should find useful.

  # Forward email to your server
  ipfwadm -F -a accept -b -P tcp -S 1024:65535 -D 25

  # Forward email connections to outside email servers
  ipfwadm -F -a accept -b -P tcp -S 25 -D 1024:65535

  # Forward Web connections to your Web Server
  /sbin/ipfwadm -F -a accept -b -P tcp -S 1024:65535 -D 80

  # Forward Web connections to outside Web Server
  /sbin/ipfwadm -F -a accept -b -P tcp -S 196.1.2.* 80 -D 1024:65535 

  # Forward DNS traffic
  /sbin/ipfwadm -F -a accept -b -P udp -S 53 -D

You might also be interested in accounting for traffic going through your firewall. This script will count ever packet. You could add a line or to to account for packets going to just a single system.

  # Flush the current accounting rules
  ipfwadm -A -f
  # Accounting
  /sbin/ipfwadm -A -f
  /sbin/ipfwadm -A out -i -S -D
  /sbin/ipfwadm -A out -i -S -D
  /sbin/ipfwadm -A in -i -S -D
  /sbin/ipfwadm -A in -i -S -D

If all you wanted was a filtering firewall you can stop here. Enjoy :-)

Previous Next Contents