IPFW Firewall Explained
Created: 09/03/2005
General Information
This guide helps you setup a simple firewall for any FreeBSD version (recent) that can load the firewall module.Introduction
Why have protection? Computers on the Internet run the risk of being damaged or hijacked. Firewall software is a very powerful tool in fighting this. Having firewall software doesn't mean that your safe. You will still have to update your system in order to fix security bugs and check for viruses. Although the latter isn't much of a problem for Unix-like computers at the time of writing.Notes
The newer versions of FreeBSD can load the firewall software when this is required. Older versions of FreeBSD don't have this ability and need to have a kernel compiles. You also need to do this with the newer version when you like to create more advanced rules, like logging of traffic shaping.Summary
The firewall and the natd daemon are loaded by adding the following lines to/etc/rc.conf:|
firewall_enable="YES" firewall_quiet="NO" firewall_type="/etc/firewall.conf" |
|
# check ip packed agains state add 100 check-state # allow local traffic add 160 allow ip from any to any via lo0 out add 1010 allow ip from 192.168.1.0/24 to 192.168.1.0/24 via xl0 add 4980 allow ip from any to any in via xl0 # pass packets comming from internal NIC and us add 2000 skipto 2100 ip from any to any via xl0 add 2000 skipto 2100 ip from 192.168.1.0/24 to any via xl1 out add 2000 skipto 2100 ip from any to 192.168.1.0/24 via xl1 in add 2000 skipto 2100 ip from 10.0.0.0/24 to any via xl1 in add 2000 skipto 2100 ip from any to 10.0.0.0/24 via xl1 out # deny spoofing add 2010 deny ip from 10.0.0.0/8 to any in add 2010 deny ip from 127.0.0.0/8 to any in add 2010 deny ip from 172.0.0.0/12 to any in add 2010 deny ip from 192.0.2.0/24 to any in add 2010 deny ip from 192.168.0.0/16 to any in add 2020 reject ip from any to 10.0.0.0/8 out add 2020 reject ip from any to 127.0.0.0/8 out add 2020 reject ip from any to 172.0.0.0/12 out add 2020 reject ip from any to 192.0.2.0/24 out add 2020 reject ip from any to 192.168.0.0/16 out # stateful firewall add 4990 allow tcp from any to any out via xl1 setup keep-state add 4990 allow udp from any to any out via xl1 keep-state add 4990 allow icmp from any to any out via xl1 keep-state # reject (unreach host) outgoing so we know and don't have to wait add 4997 reject ip from any to any in via xl0 add 4998 reject ip from any to any out via xl1 # deny incoming for stealth add 4999 deny ip from any to any |
Loading the Firewall Rules
The first step is about loading the rules. FreeBSD loads the rules during the boot process. It needs some information in order to load the rules. It looks for that information in/etc/rc.conf. Open it and the following lines.|
firewall_enable="YES" firewall_quiet="NO" firewall_type="/etc/firewall.conf" |
firewall_enabled is true, by loading the rules as indicated by file_type. If firewall_quiet isn't set or is set to NO, then the rules will be printed to the screen duing boot.Configuring the Firewall Rules
The rules need to be written down in/etc/firewall.conf, since the previous section set ipfw up to load this file. This section explains first how the IP packets are passed though and matched against the rules, before we go into the rules.Stateful Firewall
The first thing on the agenda is to setup a security barrier between the Internet and this computer. The task is to allow desired packets by this computer. The stateful firewall is just the tool to use.|
# stateful firewall add 4990 allow tcp from any to any out via xl1 setup keep-state add 4990 allow udp from any to any out via xl1 keep-state add 4990 allow icmp from any to any out via xl1 keep-state |
|
# check ip packed agains state add 100 check-state |
Network Adress Translation
Please skip this section if you didn't compile your own kernel or have no idea what that is. This section is intented to be informational only and was added because that is the primary goal of this guide.|
# select traffic for natd add 3000 skipto 3400 ip from any to any via xl0 add 3000 skipto 3400 ip from me to any via xl1 add 3210 divert 8668 ip from any to any add 3220 skipto 3400 ip from any to me add 3390 allow ip from any to any |
Deny Spoofing
|
# deny spoofing add 2010 deny ip from 10.0.0.0/8 to any in add 2010 deny ip from 127.0.0.0/8 to any in add 2010 deny ip from 172.0.0.0/12 to any in add 2010 deny ip from 192.0.2.0/24 to any in add 2010 deny ip from 192.168.0.0/16 to any in add 2020 reject ip from any to 10.0.0.0/8 out add 2020 reject ip from any to 127.0.0.0/8 out add 2020 reject ip from any to 172.0.0.0/12 out add 2020 reject ip from any to 192.0.2.0/24 out add 2020 reject ip from any to 192.168.0.0/16 out |
|
# pass packets comming from internal NIC and us add 2000 skipto 2100 ip from any to any via xl0 add 2000 skipto 2100 ip from 192.168.1.0/24 to any via xl1 out add 2000 skipto 2100 ip from any to 192.168.1.0/24 via xl1 in add 2000 skipto 2100 ip from 10.0.0.0/24 to any via xl1 in add 2000 skipto 2100 ip from any to 10.0.0.0/24 via xl1 out |
Allow Local Traffic
Next are rules for that allow all traffic from and to the local LAN. There is no need to use natd or the stateful firewall here and doing so would put a strain on their resources. I have seen cases where heavy use of the LAN resulted in natd taking up 100% of the CPU. This will prevent that from occurring.|
# allow local traffic add 160 allow ip from any to any via lo0 out add 1010 allow ip from 192.168.1.0/24 to 192.168.1.0/24 via xl0 |
Deny Everything Else
All other packets that goes on to the Internet are denied or rejected.|
# allow traffic on the internal NIC add 4980 allow ip from any to any in via xl0 # reject (unreach host) outgoing so we know and don't have to wait add 4997 reject ip from any to any in via xl0 add 4998 reject ip from any to any out via xl1 # deny incomming for stealth add 4999 deny ip from any to any |
Final Notes
Using firewall protection is important because it provides a powerfull security that gives protection from attacks. Everyone should have one on each computer. Only the most basic stuff was discussed in this guide. The firewall may not be to your liking the way it is; however, I hope I've given enough guidance and support to make some changes for yourself. In addition, I'd like to encourage you to contact me, if you have questions or feedback about this guide.This guide is © 2005 - 2009 alfatrion.
Author: alfatrion