<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://freebsdwiki.net/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://freebsdwiki.net/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=173.88.199.104</id>
		<title>FreeBSDwiki - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://freebsdwiki.net/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=173.88.199.104"/>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Special:Contributions/173.88.199.104"/>
		<updated>2026-09-09T13:12:59Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.18.0</generator>

	<entry>
		<id>http://freebsdwiki.net/index.php/Disaster_Recovery_Scheme</id>
		<title>Disaster Recovery Scheme</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Disaster_Recovery_Scheme"/>
				<updated>2012-08-14T00:50:58Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Dump To Remote Host */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Disaster Recovery Scheme ==&lt;br /&gt;
&lt;br /&gt;
The biggest error made by computer users is ignoring the importance of backing up their systems. Now for home personal systems where there is no real user data or the system is not used as the major vehicle to interface with the Internet, then a backup of your hard drive may not be necessary. But if you have a desktop installed or more than a few ports installed ‘IT IS NECESSARY’ to make a backup of your customized system as reinstalling everything from scratch is much more of a effort than just restoring the backup of your system. Professional users have an even more pressing need for back ups as they are generally required to provide 7/24 available which means they do not have time to reinstall and configure from scratch. &lt;br /&gt;
&lt;br /&gt;
The second biggest error made by computer users is not testing the restore process before it’s required in an emergence caused my hard drive failure or security compromise of the system or just plain stupid administration operator errors. Having a pre-tested documented restore process is essential in a professional 7/24 available environment. Nothing is worse then trying to use your backup files and finding out they are un-usable or incomplete. This is why a Disaster Recovery simulation is so necessary.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Two Categories of Backups ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
System Backups&lt;br /&gt;
&lt;br /&gt;
System backups mean the file systems where the operating system runs from. The basic system runs out of the / file system and uses the /var file system. The /usr file system is where all the ports are run from and installed to as well as any system sources if installed. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
User Data Backups&lt;br /&gt;
&lt;br /&gt;
User data backups means backing up the data created by the users. Normally the directory /usr/home/user is the location for desktop application created user data files such as spreadsheet files or text files or document files. The location of Database data may also be a directory under /usr. This is ok on systems with only a few users and databases used for developing applications or only containing small amounts of data. Since ‘dump’ only works on ‘file systems’, dumping the /usr file system will back them up. &lt;br /&gt;
&lt;br /&gt;
Backing up user data separate from the system has it logical benefits. In environments containing a large number of active users and production databases it’s desirable to allocate separate file systems for /usr/home and /myDataBase on the same hard drive or on a separate hard drive depending on their size. Backing up Database data with the dump command should not be the only mode of database backup. All database software has it’s own dump and restore functions and should be used separately and in addition to the dump command generated backups. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Dump Command ==&lt;br /&gt;
&lt;br /&gt;
The dump command is designed for writing an complete image of non-mounted file systems. Dump cannot select a directory tree or individual files to dump. Non-mounted file systems means ‘currently not in use’. When you are running in MUM (multi-user-mode) there are services and other background processes (IE: sendmail or postfix, cron, DHCP, apache, mysql.) changing the content of the files in the live file system being dumped. Trying to dump the file systems that your system is running from may result in dumping files that are changing in flight thus causing the dump file to contain &amp;quot;time skew&amp;quot; corruption making it un-usable for restore. There are 2 ways to eliminate this problem.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Multiple-User-Mode ==&lt;br /&gt;
&lt;br /&gt;
In MUM (multiple-user-mode) which is the normal way FreeBSD runs, you give the ‘dump command’ the –L flag. This tells dump that its going to be reading a live file system and to first take a snapshot of the file system in the .snap directory of that file system. The point of the snapshot is to give a point-in-time consistent view of the filesystem. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Single-User-Mode ==&lt;br /&gt;
&lt;br /&gt;
The alternate method is by booting your system in SUM (single-user-mode). In SUM your system is isolated from the network, all file systems are un-mounted except / which is in read only mode and there is only one active process in your system, the root shell you are using to dump and or restore the file systems. In this environment you leave off the –L flag from the dump command. &lt;br /&gt;
&lt;br /&gt;
To enter SUM:&lt;br /&gt;
During the normal boot process, it pauses for 10 seconds at the FreeBSD menu. &lt;br /&gt;
At this pause select option 4 for single user mode: &lt;br /&gt;
At the prompt for shell path, just hit enter on keyboard. &lt;br /&gt;
When the system comes up you are in SUM, all file systems are un-mounted except / which is mounted read only. &lt;br /&gt;
&lt;br /&gt;
Then issue the dump command writing the dump of each individual file system out to another motherboard cabled hard drive or USB cabled hard drive or USB memory stick, or to old fashion tape drive.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Pros and Cons of SUM verse MUM for dump processing&lt;br /&gt;
MUM Pros: Can target remote hosts as depository of the backup&lt;br /&gt;
          file directly while the dump is running.&lt;br /&gt;
          No schedule problem of when to run the dump.&lt;br /&gt;
    Cons: The –L flag will cause dump to hesitate while the &lt;br /&gt;
          snapshot is taken. &lt;br /&gt;
          The length of this hesitation depends on what services &lt;br /&gt;
          and or applications are running on the file system &lt;br /&gt;
          being dumped. &lt;br /&gt;
          These services and or applications may also pause or&lt;br /&gt;
          stop momentarily.&lt;br /&gt;
&lt;br /&gt;
SUM Pros: Minimally shorter elapse running time because&lt;br /&gt;
          no snapshot taken.&lt;br /&gt;
    Cons: On busy production systems may not be able to schedule  &lt;br /&gt;
          reboot time to do dump.&lt;br /&gt;
          Cannot target remote hosts as depository of the  &lt;br /&gt;
          backup file directly while the dump is running.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== In summary ==&lt;br /&gt;
&lt;br /&gt;
There is no technical reason for selecting to do SUM dumps verse MUM –L dumps. In both cases the resulting dump files are equivalent. Writing SUM dump files to a local host backup file system and when MUM is resumed transferring them to a remote host depository duplicates the function MUM –L has of being able to target remote hosts as depository of the backup file directly while the dump is running. Dumping and restoring requires a (Driver) running system. It does not matter if the (Driver system) is the system booted from the motherboard cabled hard drive, USB external cabled hard drive or USB memory stick containing a full installed system or booted from CD disk containing the livefs system. The livefs is burned to CD from the livefs.iso. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How often to dump ==&lt;br /&gt;
&lt;br /&gt;
You can do full dumps and dumps of just what has changed since the last time a file was dumped. These are called full dumps and change dumps. The documentation refers to them as level 0 for full dump and level 1-9 as the change dumps. The man pages give a complicated scheme for managing full and change dumps. Probably most people really need only a level 0 and a level 1, maybe a level 2.&lt;br /&gt;
&lt;br /&gt;
Basically the point of the change dumps is to make smaller backup files, which takes less time and less media. You only make the full dump (level 0) once every week or every month - whatever you’re needs are. Then in between you only dump the files that have changed since the last full dump. If that change dump file gets too big as well, then you jump to the next level on change dump. So, you do a level 0, then, the next day a level 1. If it is small (meaning only a relatively few files have changed) then the third day you still make a level 1. If the level 1 dump is now real big (meaning a lot of files changed) then on day 4 you go to a level 2 dump, etc. It is probably a good idea to regularize the process of choosing levels. That is why the man page has such a complicated scheme that covers all conditions. But most people with a personal or office/department level server often need only a regular full (level 0) dump, plus a daily level 1 dump in between the full dumps. In fact, many servers are small enough that a daily level 0 dump is all that is needed.&lt;br /&gt;
&lt;br /&gt;
Now, if you have a big system with lots of new files and changed files all the time, then you will have to organize your dumps in a more sophisticated manner. Generally, level 0 dumps take whatever amount of media they need to contain the whole filesystem. Then, for the change dumps (level 1..9) you hope to keep then to only one unit of media. If a change dump goes over one unit of media, then you move up a level the next time. The same goes for if the change dump starts to take a lot of extra time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Where To Write The Dump ==&lt;br /&gt;
&lt;br /&gt;
Your backup dumps are the single most critical item in your Disaster Recovery Scheme. Your level 1...9 change dumps must be associated with their full dump level0 companion. Now in a large data environment having massive user data changed it might take two 3gb tapes to hold the full level0 dump and a single 3gb tape partly full for each daily level1 dump. A naming convention would be used in labeling the tapes to associate the dump0 tapes with it's dump1 tapes. When using a remote or local large hard drive for storage of your dumps a suitable naming convention would also suffice. The problem with using a central depository for all your dump files is when that device fails all your backups go with it. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How Many Backup Copies Do You Keep ==&lt;br /&gt;
&lt;br /&gt;
In a professional Disaster Recovery Scheme environment separate media would be used to contain the files in a son, father, grandfather rotation. For example, The production system is froze, meaning no new ports are being installed. The system (IE /, /var. /usr) gets a level0 dump every Sunday night. Each Sunday nights dump is written to a 4GB USB stick. The first Sunday this is the son backup. The second Sunday, the same level0 dump is written to a new USB stick as the son and the previous son becomes the father. On the third Sunday, The same level0 dump is written to a new USB stick as the son and the previous son becomes the father and the previous father becomes the grandfather. On the fourth Sunday, The previous grandfather is written as the new son and each one bumps over one in rotation. The advantage to this rotation is upon restoring you find the son USB stick has a hardware problem or what you are trying to recover is not on that version of the backup you always have the father and grandfather version as safe guards.&lt;br /&gt;
&lt;br /&gt;
Another example. Using the same frozen system we have /usr/home and /database as a separate file system to segregate the user data from the system data. The system hosts hundreds of login accounts for a college computer programming students. We still do the Sunday night Full level0 dump to the son 32GB USB stick and an six nightly level1 dumps to the same son USB stick. We rotate through the 3 USB sticks as for the system dumps the only difference is the USB stick contains the dump0 and it's companion change level1 change dumps.&lt;br /&gt;
&lt;br /&gt;
Modify this rotation concept to fit you particular needs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Dump To Remote Host ==&lt;br /&gt;
&lt;br /&gt;
Dump doesn't cross file systems. In a typical FreeBSD install, /, /var, and /usr are separate file systems. A dump of / won't get them all at once. You have to issue dump command for each individual file systems, IE: /, /tmp. /var, and /usr. If you have a configuration where everything goes into a big /, dumping it will get all data from that partition. Slices and MBR are out of dump's scope.&lt;br /&gt;
&lt;br /&gt;
Here is sample commands to dump from a host running in MUM (multiple-user-mode) and storing the dump file on a remote host. To make the identification of the dumps easier the first sample commands make copies of the partition label, the last boot messages, the MBR (Master Boot Record), and the fstab file. Port 777 on localhost is a pipe to the remote host with a 800 GB hard drive.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bsdlabel ad4s1   | ssh -p 777 tom@remotehost dd of=/bkup/bsdlabel_ad4s1&lt;br /&gt;
dmesg -a         | ssh -p 777 tom@remotehost dd of=/bkup/dmesg&lt;br /&gt;
dd if=/dev/ad4 count=1 | ssh -p 777 tom@remotehost dd of=/bkup/MBR&lt;br /&gt;
cat /etc/fstab         | ssh -p 777 tom@remotehost dd of=/bkup/fstab&lt;br /&gt;
dump -0Lan -f - / | gzip | ssh -p 777 tom@remotehost dd of=/bkup/dump0-root.gz&lt;br /&gt;
dump -0Lan -f - /tmp  | gzip | ssh -p 777 tom@remotehost dd of=/bkup/dump0-tmp.gz&lt;br /&gt;
dump -0Lan -f - /var  | gzip | ssh -p 777 tom@remotehost dd of=/bkup/dump0-var.gz&lt;br /&gt;
dump -0Lan -f - /usr  | gzip | ssh -p 777 tom@remotehost dd of=/bkup/dump0-usr.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backup Script ==&lt;br /&gt;
&lt;br /&gt;
The following script (fbsd2dump) will use a 1GB USB stick as depository for the dump files. A standard FreeBSD install with postfix, mysql, apache, php only consumes 600MG in dump format and only took 13 minutes to dump. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# This script will use dump command to backup your running&lt;br /&gt;
# system to a USB flash stick.&lt;br /&gt;
# This is run as root. &lt;br /&gt;
#&lt;br /&gt;
# Change these device unit pre-fixs in the code below as needed&lt;br /&gt;
# ad0 is the live file system&lt;br /&gt;
# da0 is the target&lt;br /&gt;
#&lt;br /&gt;
# Comment or uncomment the 4 dump statements depending on &lt;br /&gt;
# whether you want to compress the saved dump file.&lt;br /&gt;
#&lt;br /&gt;
# Be sure to unplug your USB stick and re-plug it in to&lt;br /&gt;
# mount it again. &lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Starting time for this live file system dump is&amp;quot;&lt;br /&gt;
date&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Prepare the target&amp;quot;&lt;br /&gt;
dd if=/dev/zero of=/dev/da0 count=4&lt;br /&gt;
fdisk -BI /dev/da0&lt;br /&gt;
bsdlabel -B -w da0s1&lt;br /&gt;
newfs -U /dev/da0s1a&lt;br /&gt;
mount /dev/da0s1a /mnt&lt;br /&gt;
cd /mnt&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Post the dump date&amp;quot;&lt;br /&gt;
date &amp;gt; date.of.dump&lt;br /&gt;
cat date.of.dump&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Post the MBR&amp;quot;&lt;br /&gt;
dd if=/dev/ad0 count=1 &amp;gt; MBR&lt;br /&gt;
#dd if=/dev/ad0 count=1 | od -c&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Collect live Slice Partition sizes and save&amp;quot;&lt;br /&gt;
bsdlabel ad0s1 &amp;gt; liveSPsizes&lt;br /&gt;
cat liveSPsizes&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Collect live file system sizes and save&amp;quot;&lt;br /&gt;
df -h &amp;gt; liveFSsizes&lt;br /&gt;
cat liveFSsizes&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Post the fstab&amp;quot;&lt;br /&gt;
cp /etc/fstab &amp;gt; fstab&lt;br /&gt;
cat /etc/fstab&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Post the script used to create this dump&amp;quot;&lt;br /&gt;
cp /root/bin/fbsd2backup &amp;gt; fbsd2backup&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Post the script used to restore this dump&amp;quot;&lt;br /&gt;
cp /root/bin/fbsd2restore &amp;gt; fbsd2restore&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Dump file system 'a' / &amp;quot;&lt;br /&gt;
dump -0Lauf dump0-root /dev/ad0s1a &lt;br /&gt;
#dump -0Lauf dump0-root /dev/ad0s1a | gzip &lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Dump file system 'd' /var &amp;quot;&lt;br /&gt;
dump -0Lauf dump0-var /dev/ad0s1d&lt;br /&gt;
#dump -0Lauf dump0-var /dev/ad0s1d | gzip&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Dump file system 'e' /tmp &amp;quot;&lt;br /&gt;
dump -0Lauf dump0-tmp /dev/ad0s1e&lt;br /&gt;
#dump -0Lauf dump0-tmp /dev/ad0s1e | gzip&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Dump file system 'f' /usr &amp;quot;&lt;br /&gt;
dump -0Lauf dump0-usr /dev/ad0s1f&lt;br /&gt;
#dump -0Lauf dump0-usr /dev/ad0s1f | gzip&lt;br /&gt;
sync&lt;br /&gt;
cd /root&lt;br /&gt;
umount /mnt&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Ending time for this live file system dump is&amp;quot;&lt;br /&gt;
date&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Script completed&amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Unplug your USB stick NOW&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Disaster Recovery Restore Script ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following script (fbsd2restore) is the companion restore script using the dump files on the USB stick created by the above fbsd2dump script. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# This script will use restore command to restore your running&lt;br /&gt;
# system from a USB flash stick containing the dumped file systems.&lt;br /&gt;
# This is run as root. &lt;br /&gt;
#&lt;br /&gt;
# Change these device unit pre-fixes in the code below as needed&lt;br /&gt;
# ad0 is the target system&lt;br /&gt;
# da0 is the USB flash stick &lt;br /&gt;
#&lt;br /&gt;
# This script assumes that the target of the restore is of same &lt;br /&gt;
# size or larger as the hard drive the saved dump file were&lt;br /&gt;
# created from and only has a single slice allocating all the space.&lt;br /&gt;
#&lt;br /&gt;
# Secondly, That the currently running Freebsd system was booted &lt;br /&gt;
# from a fixit cd or a USB cabled external hard drive or USB stick.&lt;br /&gt;
# IE: The target hard drive is not in use.&lt;br /&gt;
#&lt;br /&gt;
# Instructions to use when booting from fixit cd.&lt;br /&gt;
# use df -h to show you the mounted file systems&lt;br /&gt;
# Boot from fixit cd. Hit enter to accept English documentation. &lt;br /&gt;
# From the sysinstall main menu select fixit and then option 1 CD.&lt;br /&gt;
# You will get fixit command line prompt. enter this&lt;br /&gt;
# mount /dev/da0s1a /mnt # mount the USB stick holding the dump files&lt;br /&gt;
# cd /mnt # change into mount point&lt;br /&gt;
# cp fbsd2restore /bin/ # copy the restore script to location in path&lt;br /&gt;
# chmod 760 /bin/fbsd2restore # give script exec permissions&lt;br /&gt;
# fbsd2restore # exec the script&lt;br /&gt;
# when script finishes in about 13 minutes enter exit to return to sysinstall.&lt;br /&gt;
# Exit sysinstall and system reboots. Remove your fixit cd from the drive.&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Starting time for this file system restore is&amp;quot;&lt;br /&gt;
date&lt;br /&gt;
&lt;br /&gt;
# make target mount point&lt;br /&gt;
cd /&lt;br /&gt;
mkdir -v target&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Prepare the target&amp;quot;&lt;br /&gt;
dd if=/mnt/MBR of=/dev/ad0 count=1&lt;br /&gt;
bsdlabel -B -w ad0s1&lt;br /&gt;
&lt;br /&gt;
# You may want to edit the liveSPsizes file to change the &lt;br /&gt;
# file system allocation sizes.&lt;br /&gt;
# ee /mnt/liveSPsizes&lt;br /&gt;
&lt;br /&gt;
bsdlabel -R ad0s1 /mnt/liveSPsizes&lt;br /&gt;
newfs -U /dev/ad0s1a&lt;br /&gt;
newfs -U /dev/ad0s1d&lt;br /&gt;
newfs -U /dev/ad0s1e&lt;br /&gt;
newfs -U /dev/ad0s1f&lt;br /&gt;
&lt;br /&gt;
# The restore flag -N means &lt;br /&gt;
# Do the extraction normally, but do not actually write any changes&lt;br /&gt;
# to disk. This can be used to check the integrity of dump media&lt;br /&gt;
# or for Disaster Recovery simulation purposes.&lt;br /&gt;
#&lt;br /&gt;
# Note the -N flag IS used here and must be removed to run live&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Restore file system 'a' / &amp;quot;&lt;br /&gt;
mount -v /dev/ad0s1a /target&lt;br /&gt;
cd /target&lt;br /&gt;
restore -rvf /mnt/dump0-root&lt;br /&gt;
#restore -rNf /mnt/dump0-root &lt;br /&gt;
#gzip /mnt/dump0-root | restore -rf- &lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Restore file system 'd' /var &amp;quot;&lt;br /&gt;
mount -v /dev/ad0s1d /target/var&lt;br /&gt;
cd /target/var&lt;br /&gt;
restore -rvf /mnt/dump0-var&lt;br /&gt;
#restore -rNf /mnt/dump0-var&lt;br /&gt;
#gzip /mnt/dump0-var | restore -rf-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Restore file system 'e' /tmp &amp;quot;&lt;br /&gt;
mount -v /dev/ad0s1e /target/tmp&lt;br /&gt;
cd /target/tmp&lt;br /&gt;
restore -rvf /mnt/dump0-tmp&lt;br /&gt;
#restore -rNf /mnt/dump0-tmp&lt;br /&gt;
#gzip /mnt/dump0-tmp | restore -rf-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Restore file system 'f' /usr &amp;quot;&lt;br /&gt;
mount -v /dev/ad0s1f /target/usr&lt;br /&gt;
cd /target/usr&lt;br /&gt;
restore -rvf /mnt/dump0-usr&lt;br /&gt;
#restore -rNf /mnt/dump0-usr&lt;br /&gt;
#gzip /mnt/dump0-usr | restore -rf-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sync&lt;br /&gt;
cd /&lt;br /&gt;
&lt;br /&gt;
# Note that restore leaves a file restoresymtable in the root&lt;br /&gt;
# directory to pass information between incremental restore passes.&lt;br /&gt;
# This file should be removed when the last incremental has been restored.&lt;br /&gt;
&lt;br /&gt;
rm -v /target/usr/restoresymtable&lt;br /&gt;
rm -v /target/tmp/restoresymtable&lt;br /&gt;
rm -v /target/var/restoresymtable&lt;br /&gt;
rm -v /target/restoresymtable&lt;br /&gt;
&lt;br /&gt;
umount -v /target/usr&lt;br /&gt;
umount -v /target/tmp&lt;br /&gt;
umount -v /target/var&lt;br /&gt;
umount -v /target&lt;br /&gt;
rmdir -v target&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Ending time for this live file system dump is&amp;quot;&lt;br /&gt;
date&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Script completed&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Disaster_Recovery_Scheme</id>
		<title>Disaster Recovery Scheme</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Disaster_Recovery_Scheme"/>
				<updated>2012-08-14T00:48:12Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: Disaster Recovery Scheme&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Disaster Recovery Scheme ==&lt;br /&gt;
&lt;br /&gt;
The biggest error made by computer users is ignoring the importance of backing up their systems. Now for home personal systems where there is no real user data or the system is not used as the major vehicle to interface with the Internet, then a backup of your hard drive may not be necessary. But if you have a desktop installed or more than a few ports installed ‘IT IS NECESSARY’ to make a backup of your customized system as reinstalling everything from scratch is much more of a effort than just restoring the backup of your system. Professional users have an even more pressing need for back ups as they are generally required to provide 7/24 available which means they do not have time to reinstall and configure from scratch. &lt;br /&gt;
&lt;br /&gt;
The second biggest error made by computer users is not testing the restore process before it’s required in an emergence caused my hard drive failure or security compromise of the system or just plain stupid administration operator errors. Having a pre-tested documented restore process is essential in a professional 7/24 available environment. Nothing is worse then trying to use your backup files and finding out they are un-usable or incomplete. This is why a Disaster Recovery simulation is so necessary.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Two Categories of Backups ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
System Backups&lt;br /&gt;
&lt;br /&gt;
System backups mean the file systems where the operating system runs from. The basic system runs out of the / file system and uses the /var file system. The /usr file system is where all the ports are run from and installed to as well as any system sources if installed. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
User Data Backups&lt;br /&gt;
&lt;br /&gt;
User data backups means backing up the data created by the users. Normally the directory /usr/home/user is the location for desktop application created user data files such as spreadsheet files or text files or document files. The location of Database data may also be a directory under /usr. This is ok on systems with only a few users and databases used for developing applications or only containing small amounts of data. Since ‘dump’ only works on ‘file systems’, dumping the /usr file system will back them up. &lt;br /&gt;
&lt;br /&gt;
Backing up user data separate from the system has it logical benefits. In environments containing a large number of active users and production databases it’s desirable to allocate separate file systems for /usr/home and /myDataBase on the same hard drive or on a separate hard drive depending on their size. Backing up Database data with the dump command should not be the only mode of database backup. All database software has it’s own dump and restore functions and should be used separately and in addition to the dump command generated backups. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Dump Command ==&lt;br /&gt;
&lt;br /&gt;
The dump command is designed for writing an complete image of non-mounted file systems. Dump cannot select a directory tree or individual files to dump. Non-mounted file systems means ‘currently not in use’. When you are running in MUM (multi-user-mode) there are services and other background processes (IE: sendmail or postfix, cron, DHCP, apache, mysql.) changing the content of the files in the live file system being dumped. Trying to dump the file systems that your system is running from may result in dumping files that are changing in flight thus causing the dump file to contain &amp;quot;time skew&amp;quot; corruption making it un-usable for restore. There are 2 ways to eliminate this problem.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Multiple-User-Mode ==&lt;br /&gt;
&lt;br /&gt;
In MUM (multiple-user-mode) which is the normal way FreeBSD runs, you give the ‘dump command’ the –L flag. This tells dump that its going to be reading a live file system and to first take a snapshot of the file system in the .snap directory of that file system. The point of the snapshot is to give a point-in-time consistent view of the filesystem. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Single-User-Mode ==&lt;br /&gt;
&lt;br /&gt;
The alternate method is by booting your system in SUM (single-user-mode). In SUM your system is isolated from the network, all file systems are un-mounted except / which is in read only mode and there is only one active process in your system, the root shell you are using to dump and or restore the file systems. In this environment you leave off the –L flag from the dump command. &lt;br /&gt;
&lt;br /&gt;
To enter SUM:&lt;br /&gt;
During the normal boot process, it pauses for 10 seconds at the FreeBSD menu. &lt;br /&gt;
At this pause select option 4 for single user mode: &lt;br /&gt;
At the prompt for shell path, just hit enter on keyboard. &lt;br /&gt;
When the system comes up you are in SUM, all file systems are un-mounted except / which is mounted read only. &lt;br /&gt;
&lt;br /&gt;
Then issue the dump command writing the dump of each individual file system out to another motherboard cabled hard drive or USB cabled hard drive or USB memory stick, or to old fashion tape drive.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Pros and Cons of SUM verse MUM for dump processing&lt;br /&gt;
MUM Pros: Can target remote hosts as depository of the backup&lt;br /&gt;
          file directly while the dump is running.&lt;br /&gt;
          No schedule problem of when to run the dump.&lt;br /&gt;
    Cons: The –L flag will cause dump to hesitate while the &lt;br /&gt;
          snapshot is taken. &lt;br /&gt;
          The length of this hesitation depends on what services &lt;br /&gt;
          and or applications are running on the file system &lt;br /&gt;
          being dumped. &lt;br /&gt;
          These services and or applications may also pause or&lt;br /&gt;
          stop momentarily.&lt;br /&gt;
&lt;br /&gt;
SUM Pros: Minimally shorter elapse running time because&lt;br /&gt;
          no snapshot taken.&lt;br /&gt;
    Cons: On busy production systems may not be able to schedule  &lt;br /&gt;
          reboot time to do dump.&lt;br /&gt;
          Cannot target remote hosts as depository of the  &lt;br /&gt;
          backup file directly while the dump is running.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== In summary ==&lt;br /&gt;
&lt;br /&gt;
There is no technical reason for selecting to do SUM dumps verse MUM –L dumps. In both cases the resulting dump files are equivalent. Writing SUM dump files to a local host backup file system and when MUM is resumed transferring them to a remote host depository duplicates the function MUM –L has of being able to target remote hosts as depository of the backup file directly while the dump is running. Dumping and restoring requires a (Driver) running system. It does not matter if the (Driver system) is the system booted from the motherboard cabled hard drive, USB external cabled hard drive or USB memory stick containing a full installed system or booted from CD disk containing the livefs system. The livefs is burned to CD from the livefs.iso. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How often to dump ==&lt;br /&gt;
&lt;br /&gt;
You can do full dumps and dumps of just what has changed since the last time a file was dumped. These are called full dumps and change dumps. The documentation refers to them as level 0 for full dump and level 1-9 as the change dumps. The man pages give a complicated scheme for managing full and change dumps. Probably most people really need only a level 0 and a level 1, maybe a level 2.&lt;br /&gt;
&lt;br /&gt;
Basically the point of the change dumps is to make smaller backup files, which takes less time and less media. You only make the full dump (level 0) once every week or every month - whatever you’re needs are. Then in between you only dump the files that have changed since the last full dump. If that change dump file gets too big as well, then you jump to the next level on change dump. So, you do a level 0, then, the next day a level 1. If it is small (meaning only a relatively few files have changed) then the third day you still make a level 1. If the level 1 dump is now real big (meaning a lot of files changed) then on day 4 you go to a level 2 dump, etc. It is probably a good idea to regularize the process of choosing levels. That is why the man page has such a complicated scheme that covers all conditions. But most people with a personal or office/department level server often need only a regular full (level 0) dump, plus a daily level 1 dump in between the full dumps. In fact, many servers are small enough that a daily level 0 dump is all that is needed.&lt;br /&gt;
&lt;br /&gt;
Now, if you have a big system with lots of new files and changed files all the time, then you will have to organize your dumps in a more sophisticated manner. Generally, level 0 dumps take whatever amount of media they need to contain the whole filesystem. Then, for the change dumps (level 1..9) you hope to keep then to only one unit of media. If a change dump goes over one unit of media, then you move up a level the next time. The same goes for if the change dump starts to take a lot of extra time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Where To Write The Dump ==&lt;br /&gt;
&lt;br /&gt;
Your backup dumps are the single most critical item in your Disaster Recovery Scheme. Your level 1...9 change dumps must be associated with their full dump level0 companion. Now in a large data environment having massive user data changed it might take two 3gb tapes to hold the full level0 dump and a single 3gb tape partly full for each daily level1 dump. A naming convention would be used in labeling the tapes to associate the dump0 tapes with it's dump1 tapes. When using a remote or local large hard drive for storage of your dumps a suitable naming convention would also suffice. The problem with using a central depository for all your dump files is when that device fails all your backups go with it. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How Many Backup Copies Do You Keep ==&lt;br /&gt;
&lt;br /&gt;
In a professional Disaster Recovery Scheme environment separate media would be used to contain the files in a son, father, grandfather rotation. For example, The production system is froze, meaning no new ports are being installed. The system (IE /, /var. /usr) gets a level0 dump every Sunday night. Each Sunday nights dump is written to a 4GB USB stick. The first Sunday this is the son backup. The second Sunday, the same level0 dump is written to a new USB stick as the son and the previous son becomes the father. On the third Sunday, The same level0 dump is written to a new USB stick as the son and the previous son becomes the father and the previous father becomes the grandfather. On the fourth Sunday, The previous grandfather is written as the new son and each one bumps over one in rotation. The advantage to this rotation is upon restoring you find the son USB stick has a hardware problem or what you are trying to recover is not on that version of the backup you always have the father and grandfather version as safe guards.&lt;br /&gt;
&lt;br /&gt;
Another example. Using the same frozen system we have /usr/home and /database as a separate file system to segregate the user data from the system data. The system hosts hundreds of login accounts for a college computer programming students. We still do the Sunday night Full level0 dump to the son 32GB USB stick and an six nightly level1 dumps to the same son USB stick. We rotate through the 3 USB sticks as for the system dumps the only difference is the USB stick contains the dump0 and it's companion change level1 change dumps.&lt;br /&gt;
&lt;br /&gt;
Modify this rotation concept to fit you particular needs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Dump To Remote Host ==&lt;br /&gt;
&lt;br /&gt;
Dump doesn't cross file systems. In a typical FreeBSD install, /, /var, and /usr are separate file systems. A dump of / won't get them all at once. You have to issue dump command for each individual file systems, IE: /, /tmp. /var, and /usr. If you have a configuration where everything goes into a big /, dumping it will get all data from that partition. Slices and MBR are out of dump's scope.&lt;br /&gt;
&lt;br /&gt;
Here is sample commands to dump from a host running in MUM (multiple-user-mode) and storing the dump file on a remote host. To make the identification of the dumps easier the first sample commands make copies of the partition label, the last boot messages, the MBR (Master Boot Record), and the fstab file. Port 777 on localhost is a pipe to the remote host with a 800 GB hard drive.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bsdlabel ad4s1                     | ssh -p 777 tom@remotehost dd of=/bkup/bsdlabel_ad4s1&lt;br /&gt;
dmesg -a                              | ssh -p 777 tom@remotehost dd of=/bkup/dmesg&lt;br /&gt;
dd if=/dev/ad4 count=1        | ssh -p 777 tom@remotehost dd of=/bkup/MBR&lt;br /&gt;
cat /etc/fstab                        | ssh -p 777 tom@remotehost dd of=/bkup/fstab&lt;br /&gt;
dump -0Lan -f - /        | gzip | ssh -p 777 tom@remotehost dd of=/bkup/dump0-root.gz&lt;br /&gt;
dump -0Lan -f - /tmp  | gzip | ssh -p 777 tom@remotehost dd of=/bkup/dump0-tmp.gz&lt;br /&gt;
dump -0Lan -f - /var   | gzip | ssh -p 777 tom@remotehost dd of=/bkup/dump0-var.gz&lt;br /&gt;
dump -0Lan -f - /usr   | gzip | ssh -p 777 tom@remotehost dd of=/bkup/dump0-usr.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Backup Script ==&lt;br /&gt;
&lt;br /&gt;
The following script (fbsd2dump) will use a 1GB USB stick as depository for the dump files. A standard FreeBSD install with postfix, mysql, apache, php only consumes 600MG in dump format and only took 13 minutes to dump. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# This script will use dump command to backup your running&lt;br /&gt;
# system to a USB flash stick.&lt;br /&gt;
# This is run as root. &lt;br /&gt;
#&lt;br /&gt;
# Change these device unit pre-fixs in the code below as needed&lt;br /&gt;
# ad0 is the live file system&lt;br /&gt;
# da0 is the target&lt;br /&gt;
#&lt;br /&gt;
# Comment or uncomment the 4 dump statements depending on &lt;br /&gt;
# whether you want to compress the saved dump file.&lt;br /&gt;
#&lt;br /&gt;
# Be sure to unplug your USB stick and re-plug it in to&lt;br /&gt;
# mount it again. &lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Starting time for this live file system dump is&amp;quot;&lt;br /&gt;
date&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Prepare the target&amp;quot;&lt;br /&gt;
dd if=/dev/zero of=/dev/da0 count=4&lt;br /&gt;
fdisk -BI /dev/da0&lt;br /&gt;
bsdlabel -B -w da0s1&lt;br /&gt;
newfs -U /dev/da0s1a&lt;br /&gt;
mount /dev/da0s1a /mnt&lt;br /&gt;
cd /mnt&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Post the dump date&amp;quot;&lt;br /&gt;
date &amp;gt; date.of.dump&lt;br /&gt;
cat date.of.dump&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Post the MBR&amp;quot;&lt;br /&gt;
dd if=/dev/ad0 count=1 &amp;gt; MBR&lt;br /&gt;
#dd if=/dev/ad0 count=1 | od -c&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Collect live Slice Partition sizes and save&amp;quot;&lt;br /&gt;
bsdlabel ad0s1 &amp;gt; liveSPsizes&lt;br /&gt;
cat liveSPsizes&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Collect live file system sizes and save&amp;quot;&lt;br /&gt;
df -h &amp;gt; liveFSsizes&lt;br /&gt;
cat liveFSsizes&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Post the fstab&amp;quot;&lt;br /&gt;
cp /etc/fstab &amp;gt; fstab&lt;br /&gt;
cat /etc/fstab&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Post the script used to create this dump&amp;quot;&lt;br /&gt;
cp /root/bin/fbsd2backup &amp;gt; fbsd2backup&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Post the script used to restore this dump&amp;quot;&lt;br /&gt;
cp /root/bin/fbsd2restore &amp;gt; fbsd2restore&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Dump file system 'a' / &amp;quot;&lt;br /&gt;
dump -0Lauf dump0-root /dev/ad0s1a &lt;br /&gt;
#dump -0Lauf dump0-root /dev/ad0s1a | gzip &lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Dump file system 'd' /var &amp;quot;&lt;br /&gt;
dump -0Lauf dump0-var /dev/ad0s1d&lt;br /&gt;
#dump -0Lauf dump0-var /dev/ad0s1d | gzip&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Dump file system 'e' /tmp &amp;quot;&lt;br /&gt;
dump -0Lauf dump0-tmp /dev/ad0s1e&lt;br /&gt;
#dump -0Lauf dump0-tmp /dev/ad0s1e | gzip&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Dump file system 'f' /usr &amp;quot;&lt;br /&gt;
dump -0Lauf dump0-usr /dev/ad0s1f&lt;br /&gt;
#dump -0Lauf dump0-usr /dev/ad0s1f | gzip&lt;br /&gt;
sync&lt;br /&gt;
cd /root&lt;br /&gt;
umount /mnt&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Ending time for this live file system dump is&amp;quot;&lt;br /&gt;
date&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Script completed&amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Unplug your USB stick NOW&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Disaster Recovery Restore Script ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following script (fbsd2restore) is the companion restore script using the dump files on the USB stick created by the above fbsd2dump script. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
# This script will use restore command to restore your running&lt;br /&gt;
# system from a USB flash stick containing the dumped file systems.&lt;br /&gt;
# This is run as root. &lt;br /&gt;
#&lt;br /&gt;
# Change these device unit pre-fixes in the code below as needed&lt;br /&gt;
# ad0 is the target system&lt;br /&gt;
# da0 is the USB flash stick &lt;br /&gt;
#&lt;br /&gt;
# This script assumes that the target of the restore is of same &lt;br /&gt;
# size or larger as the hard drive the saved dump file were&lt;br /&gt;
# created from and only has a single slice allocating all the space.&lt;br /&gt;
#&lt;br /&gt;
# Secondly, That the currently running Freebsd system was booted &lt;br /&gt;
# from a fixit cd or a USB cabled external hard drive or USB stick.&lt;br /&gt;
# IE: The target hard drive is not in use.&lt;br /&gt;
#&lt;br /&gt;
# Instructions to use when booting from fixit cd.&lt;br /&gt;
# use df -h to show you the mounted file systems&lt;br /&gt;
# Boot from fixit cd. Hit enter to accept English documentation. &lt;br /&gt;
# From the sysinstall main menu select fixit and then option 1 CD.&lt;br /&gt;
# You will get fixit command line prompt. enter this&lt;br /&gt;
# mount /dev/da0s1a /mnt # mount the USB stick holding the dump files&lt;br /&gt;
# cd /mnt # change into mount point&lt;br /&gt;
# cp fbsd2restore /bin/ # copy the restore script to location in path&lt;br /&gt;
# chmod 760 /bin/fbsd2restore # give script exec permissions&lt;br /&gt;
# fbsd2restore # exec the script&lt;br /&gt;
# when script finishes in about 13 minutes enter exit to return to sysinstall.&lt;br /&gt;
# Exit sysinstall and system reboots. Remove your fixit cd from the drive.&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Starting time for this file system restore is&amp;quot;&lt;br /&gt;
date&lt;br /&gt;
&lt;br /&gt;
# make target mount point&lt;br /&gt;
cd /&lt;br /&gt;
mkdir -v target&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Prepare the target&amp;quot;&lt;br /&gt;
dd if=/mnt/MBR of=/dev/ad0 count=1&lt;br /&gt;
bsdlabel -B -w ad0s1&lt;br /&gt;
&lt;br /&gt;
# You may want to edit the liveSPsizes file to change the &lt;br /&gt;
# file system allocation sizes.&lt;br /&gt;
# ee /mnt/liveSPsizes&lt;br /&gt;
&lt;br /&gt;
bsdlabel -R ad0s1 /mnt/liveSPsizes&lt;br /&gt;
newfs -U /dev/ad0s1a&lt;br /&gt;
newfs -U /dev/ad0s1d&lt;br /&gt;
newfs -U /dev/ad0s1e&lt;br /&gt;
newfs -U /dev/ad0s1f&lt;br /&gt;
&lt;br /&gt;
# The restore flag -N means &lt;br /&gt;
# Do the extraction normally, but do not actually write any changes&lt;br /&gt;
# to disk. This can be used to check the integrity of dump media&lt;br /&gt;
# or for Disaster Recovery simulation purposes.&lt;br /&gt;
#&lt;br /&gt;
# Note the -N flag IS used here and must be removed to run live&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Restore file system 'a' / &amp;quot;&lt;br /&gt;
mount -v /dev/ad0s1a /target&lt;br /&gt;
cd /target&lt;br /&gt;
restore -rvf /mnt/dump0-root&lt;br /&gt;
#restore -rNf /mnt/dump0-root &lt;br /&gt;
#gzip /mnt/dump0-root | restore -rf- &lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Restore file system 'd' /var &amp;quot;&lt;br /&gt;
mount -v /dev/ad0s1d /target/var&lt;br /&gt;
cd /target/var&lt;br /&gt;
restore -rvf /mnt/dump0-var&lt;br /&gt;
#restore -rNf /mnt/dump0-var&lt;br /&gt;
#gzip /mnt/dump0-var | restore -rf-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Restore file system 'e' /tmp &amp;quot;&lt;br /&gt;
mount -v /dev/ad0s1e /target/tmp&lt;br /&gt;
cd /target/tmp&lt;br /&gt;
restore -rvf /mnt/dump0-tmp&lt;br /&gt;
#restore -rNf /mnt/dump0-tmp&lt;br /&gt;
#gzip /mnt/dump0-tmp | restore -rf-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Restore file system 'f' /usr &amp;quot;&lt;br /&gt;
mount -v /dev/ad0s1f /target/usr&lt;br /&gt;
cd /target/usr&lt;br /&gt;
restore -rvf /mnt/dump0-usr&lt;br /&gt;
#restore -rNf /mnt/dump0-usr&lt;br /&gt;
#gzip /mnt/dump0-usr | restore -rf-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sync&lt;br /&gt;
cd /&lt;br /&gt;
&lt;br /&gt;
# Note that restore leaves a file restoresymtable in the root&lt;br /&gt;
# directory to pass information between incremental restore passes.&lt;br /&gt;
# This file should be removed when the last incremental has been restored.&lt;br /&gt;
&lt;br /&gt;
rm -v /target/usr/restoresymtable&lt;br /&gt;
rm -v /target/tmp/restoresymtable&lt;br /&gt;
rm -v /target/var/restoresymtable&lt;br /&gt;
rm -v /target/restoresymtable&lt;br /&gt;
&lt;br /&gt;
umount -v /target/usr&lt;br /&gt;
umount -v /target/tmp&lt;br /&gt;
umount -v /target/var&lt;br /&gt;
umount -v /target&lt;br /&gt;
rmdir -v target&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Ending time for this live file system dump is&amp;quot;&lt;br /&gt;
date&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot; &amp;quot;&lt;br /&gt;
echo &amp;quot;Script completed&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Personal_Scripts</id>
		<title>Personal Scripts</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Personal_Scripts"/>
				<updated>2012-08-14T00:39:19Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: Created page with &amp;quot;  == Personal Scripts ==  FBSD is a command line driven operating system. It's very hard to remember where all the config files are and what they are called. You have to be co...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Personal Scripts ==&lt;br /&gt;
&lt;br /&gt;
FBSD is a command line driven operating system. It's very hard to remember where all the config files are and what they are called. You have to be constantly working on your FBSD systems on a daily basis to remember all the commands you use to do repetitive things. I found that if I build a simple shell script and place the command I use, or the sequence of commands I used to perform repetitive tasks, I can give the script a long name that self describes what it does. All users have a bin directory which they can save their canned scripts in. The users bin directory is not created as part of the FBSD install so you must make one for root.&lt;br /&gt;
&lt;br /&gt;
cd /root&lt;br /&gt;
&lt;br /&gt;
mkdir bin&lt;br /&gt;
&lt;br /&gt;
Now lets say for example I use IPFILTER for my firewall. I repeatedly edit the filter rules, load the rules, edit the NAT rules, and load the NAT rules. I simplify these admin functions by creating simple scripts with meaningful names.&lt;br /&gt;
&lt;br /&gt;
ipf.edit.rules&lt;br /&gt;
ipf.load.rules&lt;br /&gt;
ipf.edit.natrules&lt;br /&gt;
ipf.load.natrules&lt;br /&gt;
&lt;br /&gt;
Here is how to create simple script.&lt;br /&gt;
&lt;br /&gt;
cd /root/bin&lt;br /&gt;
&lt;br /&gt;
ee ipf.edit.rules&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#! /bin/sh&lt;br /&gt;
ee /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
Close and save the file.&lt;br /&gt;
&lt;br /&gt;
ls -l     # will show you that this file only has read and write permission. It needs execute permission to run.&lt;br /&gt;
&lt;br /&gt;
chmod 700 ipf.edit.rules   # give it execute permission for root owner only&lt;br /&gt;
&lt;br /&gt;
rehash    # let the shell know about it&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enter ipf.edit.rules on the command line, and you find yourself looking at the screen displaying your rules file open and ready to edit.&lt;br /&gt;
&lt;br /&gt;
Every time you want to create another script, copy an existing one and it will already have the correct permissions to execute.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/File_Permissions</id>
		<title>File Permissions</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/File_Permissions"/>
				<updated>2012-08-14T00:32:19Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: File Permissions&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== File Permissions ==&lt;br /&gt;
&lt;br /&gt;
FBSD, is a direct descendant of the multiuser system UNIX, and has inherited the underlining permission structure that FBSD uses for the control of sharing and managing requests for hardware devices, peripherals, memory, CPU time, files and directories. Everything FBSD manages has a set of permissions governing who can read, write, and execute the resource. &lt;br /&gt;
&lt;br /&gt;
These permissions are stored as a 10 position control field.&lt;br /&gt;
&lt;br /&gt;
The format of the permission control field, (from left to right) is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Position 1 values = d   This is a directory&lt;br /&gt;
                    l   This is a link file&lt;br /&gt;
                    -   This is a file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The remaining 9 positions are broken into groups of 3 positions. The first group of 3 positions refers to the owner, the next group of 3 positions refers to the account group, and the last 3 positions refer to all other users. Any of the positions may hold a ‘-‘ dash which means no permission.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Position 2, 3, 4 = owner&lt;br /&gt;
      Position 2 can contain an R means the owner has read access&lt;br /&gt;
                                - means the owner has no read access&lt;br /&gt;
      Position 3 can contain a  W means the owner has write access&lt;br /&gt;
                                - means the owner has no write access&lt;br /&gt;
      Position 4 can contain an X means the owner has execute access&lt;br /&gt;
                                - means the owner has no execute access&lt;br /&gt;
&lt;br /&gt;
Position 5, 6, 7 = account group&lt;br /&gt;
      Position 2 can contain an R means the group has read access&lt;br /&gt;
                                - means the group has no read access&lt;br /&gt;
      Position 3 can contain a  W means the group has write access&lt;br /&gt;
                                - means the group has no write access&lt;br /&gt;
      Position 4 can contain an X means the group has execute access&lt;br /&gt;
                                - means the group has no execute access&lt;br /&gt;
&lt;br /&gt;
Position 8, 9, 10 - all users&lt;br /&gt;
      Position 2 can contain an R means all users have read access&lt;br /&gt;
                                - means all users have no read access&lt;br /&gt;
      Position 3 can contain a  W means all users have write access&lt;br /&gt;
                                - means all users have no write access&lt;br /&gt;
      Position 4 can contain an X means all users have execute access&lt;br /&gt;
                                - means all users have no execute access&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Read permission: Enables you to look at a file or directory. You can use an editor to see the content of the file. You can copy this file. If it's a directory, lets you list content of directory.&lt;br /&gt;
&lt;br /&gt;
Write permission: Enables you to change the content of the file and save it. You need write permission to the directory to delete files or create new files.&lt;br /&gt;
&lt;br /&gt;
Execute permission: Enables you to run the program or shell script contained in the file. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can use the ls -l command to view a long directory listing that displays the 10 position permission control field to the far left side of the listing. &lt;br /&gt;
&lt;br /&gt;
For example, a ls -l /etc/ &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
drwxr-xr-x 2 root wheel 512 Oct 9 2002 X11&lt;br /&gt;
-rw-r--r-- 1 root wheel 1340 Jan 7 2003 adduser.conf&lt;br /&gt;
lrwxrwxrwx 1 root wheel 12 Jan 9 2003 aliases -&amp;gt; mail/aliases&lt;br /&gt;
-rw-r--r-- 1 root wheel 65536 Jan 9 2003 aliases.db&lt;br /&gt;
-rw-r--r-- 1 root wheel 208 Oct 9 2002 amd.map&lt;br /&gt;
-rwxr-xr-x 1 root wheel 7183 Jan 7 2003 cvsupfile&lt;br /&gt;
drwxr-xr-x 2 root wheel 512 Jan 9 2003 defaults&lt;br /&gt;
-rw-r--r-- 1 root wheel 271 Oct 9 2002 dhclient.conf&lt;br /&gt;
-rw-r--r-- 1 root wheel 6990 Oct 9 2002 disktab&lt;br /&gt;
-rw-r--r-- 1 root wheel 478 Oct 9 2002 dm.conf&lt;br /&gt;
-rw-rw-r-- 1 root operator 0 Oct 9 2002 dumpdates&lt;br /&gt;
-rw-r--r-- 1 root wheel 142 Oct 9 2002 fbtab&lt;br /&gt;
-rwxr-xr-x 1 root wheel 832 Nov 10 13:13 fstab&lt;br /&gt;
-rwxr-xr-x 1 root wheel 1886 Jan 7 2003 gettytab&lt;br /&gt;
drwxr-xr-x 2 root wheel 512 Jan 9 2003 gnats&lt;br /&gt;
-rw-r--r-- 1 root wheel 477 Jul 9 18:14 group&lt;br /&gt;
-rwxr-xr-x 1 root wheel 1996 Jan 7 2003 newsyslog.conf&lt;br /&gt;
-rw------- 1 root wheel 1603 Oct 9 2002 nsmb.conf&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lets look closely at the first line in the above listing.&lt;br /&gt;
&lt;br /&gt;
drwxr-xr-x 2 root wheel 512 Oct 9 2002 X11&lt;br /&gt;
&lt;br /&gt;
The file and directory names are on the far right side. This is a directory, because the first position of the permission control field is populated with a d. The word root means the owner of the directory is root. The word wheel is the name of the account group. The permission control field says the owner root has read, write, and execute permission. The account group wheel has read and execute permission, and the same for all other users.&lt;br /&gt;
&lt;br /&gt;
Lets look at one more&lt;br /&gt;
&lt;br /&gt;
-rw-rw-r-- 1 root operator 0 Oct 9 2002 dumpdates&lt;br /&gt;
&lt;br /&gt;
Here, this is a file, because position one of the permission control field has a -. Owner root has read and write permission, account group operator has same permission as owner root, while all other users only have read permission.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Managing Permissions ==&lt;br /&gt;
&lt;br /&gt;
Root and members of the account group 'wheel' are the only users who have permission to change settings of files not belonging to themselves. The command chmod is used to change the permission settings in the permission control field. It accepts a 3 digit numerical number or a group of r's and w's as input. The 3 digit numerical number form of input is easier to understand and use. The 3 digit numerical number represents the 3 groups in the permission control field, one digit per category: owner, account group, and all other users. The permission digits are:&lt;br /&gt;
&lt;br /&gt;
Digit Permission&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
0  None&lt;br /&gt;
1  Execute Only&lt;br /&gt;
2  Write Only&lt;br /&gt;
3  Write and Execute&lt;br /&gt;
4  Read Only&lt;br /&gt;
5  Read and Execute&lt;br /&gt;
6  Read and Write&lt;br /&gt;
7  Read, Write, and Execute&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Change file permissions&lt;br /&gt;
chmod 700 dumpdates &lt;br /&gt;
&lt;br /&gt;
would assign owner read, write, and execute permissions, and account group and all other users get no permission. &lt;br /&gt;
&lt;br /&gt;
An ls -l dumpdates would show this:&lt;br /&gt;
&lt;br /&gt;
-rwx------ 1 root operator 0 Oct 9 2002 dumpdates&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
chmod 764 dumpdates &lt;br /&gt;
&lt;br /&gt;
would assign owner read, write, and execute permissions, account group gets read and write permissions, and all other users get read only permission. &lt;br /&gt;
&lt;br /&gt;
ls -l dumpdates  would show this:&lt;br /&gt;
&lt;br /&gt;
-rwxrw-r-- 1 root operator 0 Oct 9 2002 dumpdates&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Change file owner ==&lt;br /&gt;
The chown command is used to change the owner. If my personal FBSD account name was joe and I wanted to change the owner of dumpdates from root to joe, I would use this:&lt;br /&gt;
&lt;br /&gt;
chown joe dumpdates&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Change file group ==&lt;br /&gt;
The chgrp command is used the change the account group. If I wanted to change dumpdates group from operator to network, I would use this:&lt;br /&gt;
&lt;br /&gt;
chgrp network dumpdates.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/User_Account_Admin</id>
		<title>User Account Admin</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/User_Account_Admin"/>
				<updated>2012-08-14T00:19:55Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: User Account Admin&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== User Account Admin ==&lt;br /&gt;
&lt;br /&gt;
FBSD has built in commands for the administration of user accounts. FBSD only allows users that have a predefined account on the system to have access to its facilities. The account name/ID and password is what you are prompted for during the login process, whether locally from the FBSD console screen or remotely using some client application software. Email, Telnet, and FTP are some of the most popular facilities who's access is controlled by the user account. The FBSD Handbook at http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/users-modifying.html gives a very good explanation of FBSD custom account admin scripts. These scripts are implemented around the pw(8) command. The ‘man pw’ is also good.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Configure the pw Command ==&lt;br /&gt;
&lt;br /&gt;
When FBSD is first installed the pw command does not have its pw.conf option file. The pw command stills works but you have no idea what the defaults are and the command line gets full using the pw command option flags.&lt;br /&gt;
&lt;br /&gt;
The first thing you should do if you want to use the pw command to add users is to create the pw.conf file, do this by entering: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pw adduser -D   # this will create the /etc/pw.conf file.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The comments in the file are self explanatory. You have to edit /etc/pw.conf to change the defaults. I changed the default group &amp;quot;guest&amp;quot; and the additional groups to &amp;quot;mail&amp;quot;. Everybody has to belong to the &amp;quot;mail&amp;quot; group to use the sendmail server.&lt;br /&gt;
&lt;br /&gt;
My /etc/pw.conf looks like this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
# /etc/pw.conf - user/group configuration defaults&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Password for new users? no=nologin yes=loginid none=blank random=random&lt;br /&gt;
defaultpasswd = &amp;quot;yes&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Reuse gaps in uid sequence? (yes or no)&lt;br /&gt;
reuseuids = &amp;quot;yes&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Reuse gaps in gid sequence? (yes or no)&lt;br /&gt;
reusegids = &amp;quot;yes&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Path to the NIS passwd file (blank or 'no' for none)&lt;br /&gt;
nispasswd =&lt;br /&gt;
&lt;br /&gt;
# Obtain default dotfiles from this directory&lt;br /&gt;
skeleton = &amp;quot;/usr/share/skel/&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Mail this file to new user (/etc/newuser.msg or no)&lt;br /&gt;
newmail = &amp;quot;no&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Log add/change/remove information in this file&lt;br /&gt;
logfile = &amp;quot;/var/log/userlog&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Root directory in which $HOME directory is created&lt;br /&gt;
home = &amp;quot;/home&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Colon separated list of directories containing valid shells&lt;br /&gt;
shellpath = &amp;quot;/bin&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Comma separated list of available shells (without paths)&lt;br /&gt;
shells = &amp;quot;sh&amp;quot;,&amp;quot;csh&amp;quot;,&amp;quot;tcsh&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Default shell (without path)&lt;br /&gt;
defaultshell = &amp;quot;csh&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Default group (leave blank for new group per user)&lt;br /&gt;
defaultgroup = &amp;quot;guest&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Extra groups for new users&lt;br /&gt;
extragroups = &amp;quot;mail&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Default login class for new users&lt;br /&gt;
defaultclass = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Range of valid default user ids&lt;br /&gt;
minuid = 1000&lt;br /&gt;
maxuid = 32000&lt;br /&gt;
&lt;br /&gt;
# Range of valid default group ids&lt;br /&gt;
mingid = 1000&lt;br /&gt;
maxgid = 32000&lt;br /&gt;
&lt;br /&gt;
# Days after which account expires (0=disabled)&lt;br /&gt;
expire_days = 0&lt;br /&gt;
&lt;br /&gt;
# Days after which password expires (0=disabled)&lt;br /&gt;
password_days = 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Examples of pw command usage&lt;br /&gt;
pw adduser -D = create /etc/pw.conf file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pw adduser tom -m -c ‘tom brown’    # tom = userid, &lt;br /&gt;
                                    # -m = create home directory &lt;br /&gt;
                                    # -c = full name field&lt;br /&gt;
                                    # use quotes to enclose large name &lt;br /&gt;
&lt;br /&gt;
The password will be the same as the userid.&lt;br /&gt;
The user needs to use the passwd command to set their secret password.&lt;br /&gt;
&lt;br /&gt;
pw deluser tom -r         # -r = remove his home directory&lt;br /&gt;
&lt;br /&gt;
pw showuser tom           # display users entry in password file&lt;br /&gt;
&lt;br /&gt;
pw showuser tom -P        # display password info in human readable form. &lt;br /&gt;
&lt;br /&gt;
pw showuser -a            # display all entries in password file&lt;br /&gt;
&lt;br /&gt;
pw addgroup networking    # Add this new group&lt;br /&gt;
&lt;br /&gt;
pw showgroup wheel        # List all users in this group&lt;br /&gt;
&lt;br /&gt;
pw modgroup wheel -M tom  # Add user tom to group wheel &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The pw subcommands can be written in reverse order:&lt;br /&gt;
&lt;br /&gt;
adduser &amp;amp; useradd mean same thing.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
pw command embedded in a script&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#! /bin/sh&lt;br /&gt;
pw adduser tom -m -c testing -h 0 &amp;lt;&amp;lt;EOD&lt;br /&gt;
water&lt;br /&gt;
EOD&lt;br /&gt;
# water is the password to be assigned to tom &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== passwd command ==&lt;br /&gt;
The passwd command is the usual way to change your own password as a user or another user's password as the superuser root. Follow the prompts issued by the command.&lt;br /&gt;
&lt;br /&gt;
passwd tom &lt;br /&gt;
&lt;br /&gt;
This is the what is presented to the screen.&lt;br /&gt;
&lt;br /&gt;
Changing local password for tom&lt;br /&gt;
New password:&lt;br /&gt;
Retype new password:&lt;br /&gt;
passwd: updating the database&lt;br /&gt;
passwd: done&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== chpass command ==&lt;br /&gt;
The chpass command is used to change user database info such as password, shells, and personal info (such as full name, phone number, etc.) as a user or another user's info as the superuser root.&lt;br /&gt;
&lt;br /&gt;
chpass tom &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Local_Area_Network_(LAN)</id>
		<title>Local Area Network (LAN)</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Local_Area_Network_(LAN)"/>
				<updated>2012-08-14T00:09:56Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* DHCP Configuration Instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Setting up Local Area Network (LAN) ==&lt;br /&gt;
&lt;br /&gt;
A local area network (LAN) is a group of computers and associated devices that share a common communications line or wireless link and typically share the resources of a single processor or server within a small geographic area (for example, within an office building). Usually, the server has applications and data storage that are shared in common by multiple LAN computer users. A local area network may serve as few as two or three users (for example, in a home network) or as many as thousands of users (for example, in an FDDI network). Typically, a suite of application programs can be kept on the LAN server. Users who need an application frequently can download it once and then run it from their local hard disk. A user can share files with others at the LAN server. &lt;br /&gt;
&lt;br /&gt;
There are many technical limitations and options to how a LAN is configured depending on if you are a non-commercial user or commercial user and how many of the MS/Windows and/or Unix network server sharing facilities you want to enable. &lt;br /&gt;
&lt;br /&gt;
Basically what determines if you are commercial user or not is how you are known to the public Internet. A commercial user has a permanent, dedicated, high-speed leased Internet line connecting them to their ISP and one or more static IP addresses assigned by their ISP. A static IP address is always the same number; it never changes between logins to the ISP. They have an official registered domain name that points to one of the static IP address which points to their PC that is acting as their gateway. If the commercial user pays for a large block of static IP addresses then they can use these IP address for the computers on the LAN and not need to use NAT (network address translation). Their email will arrive at the gateway PC and is processed by their mail server directly. They do not use their ISP to receive and hold their email for them.&lt;br /&gt;
&lt;br /&gt;
An non-commercial user, like the home user, uses a phone line dial in login to their ISP on a limited speed connection or has a 24/7 cable or DSL high speed connection and gets assigned a single dynamic IP address which changes every time they login. Their ISP receives and holds all their email for them. The only way a public Internet user can find them is if that know the dynamic IP address currently in use by them. From the ISP viewpoint a non-commercial user uses a very small amount of its overall resources and so charges much less for a single user account.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FBSD system that is acting as the gateway can also be configured to provide different levels of network sharing depending on what kind of operating systems are running on the PCs connected to the LAN. For Unix-like operating systems NFS provides network file and device sharing, while the FBSD port application Samba does the same thing for MS/Windows PCs on the LAN. These facilities, NFS and Samba are not covered in this guide as they are more applicable to commercial users who have large LANs.&lt;br /&gt;
&lt;br /&gt;
See this link for Samba details http://us1.samba.org/samba/samba.html&lt;br /&gt;
&lt;br /&gt;
See the following links for details on FBSD NFS:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/network-nfs.html&lt;br /&gt;
&lt;br /&gt;
http://www.onlamp.com/pub/a/bsd/2000/07/26/FreeBSD_Basics.html&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Home User LANs ==&lt;br /&gt;
&lt;br /&gt;
Normally each family member would have to have their own phone line and unique ISP account to connect all of the family PCs to the Internet simultaneously. This is a costly way of doing this. The alternative is to have a single FBSD system gateway connect to an ISP and then network the other family members PC's behind the gateway using private IP addresses and NAT (Network Addressing Translation) so everything leaving the gateway system looks like it came from the single dynamic IP address assigned by the ISP. Your ISP can not tell if the packet passing through them has been NATed or not.&lt;br /&gt;
&lt;br /&gt;
Installer Note: When you sign up for service with your ISP you have to sign a user agreement that basically says you are not allowed to do NAT on your PC or run email services or web servers. If you are caught, it's grounds for them to terminate your account. Never tell your ISP tech support people what you are doing. Most ISP's leave open all the ports except the port used by an email server, which they block. More recently some ISPs have started to also block the web server port number.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another simple to configure facility is an anonymous FTP server on the FBSD gateway so LAN users can post files there that they want to pass to other LAN users. This allows them to pass large files between LAN users.&lt;br /&gt;
&lt;br /&gt;
The LAN can be populated with both MS/Windows boxes and FBSD boxes and not cause any problems. ISP's usually allow 5 email address per dial in account. Each family member can have their own email address and using the email client on their PC get their email directly from the ISP email server. Or you can run a task on the FBSD gateway box to download the email from the ISP account on a recurring schedule and store it in the FBSD built in email server called sendmail, and then have all the LAN users get their email from the sendmail server without having the FBSD gateway connected to the Internet.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Topography of a LAN&lt;br /&gt;
        __________         ________             _____ &lt;br /&gt;
       |          |       |        |           |     | &lt;br /&gt;
       |  FBSD/GW |       | switch |&amp;lt;---------&amp;gt;| LAN |&lt;br /&gt;
       |          |       | or     |           | PC1 |    _____&lt;br /&gt;
       |      NIC |&amp;lt;-----&amp;gt;| hub    |&amp;lt;--|       |_____|   |     |&lt;br /&gt;
       |__________|       |________|   |                 | LAN |&lt;br /&gt;
                                       |&amp;lt;---------------&amp;gt;| PC2 |&lt;br /&gt;
                                                         |_____| &lt;br /&gt;
                                                             &lt;br /&gt;
                                                            &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The above diagram shows a simple single LAN circuit. Your FBSD gateway box needs a NIC for each separate LAN circuit. Each circuit must use a unique IP address subnet. You cable the LAN NIC from the back of the FBSD gateway PC to a network switch or hub. A small cheap switch normally has 5 plugs. One plug for each PC on the LAN including the FBSD gateway. You run a cable from the switch to the NIC of each PC you want on the LAN. A LAN circuit can handle many PC's and many downstream switches as long as the max distance of the cable is not exceeded. To add more LAN users you create another circuit by adding another NIC in the FBSD box connected to another switch which has more LAN PC's connected to it. Please note, this is a very simplified LAN description and layout, but is adequate for basic understanding of how the physical parts of the LAN are cabled together.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For the home user with just two PCs, you can cable your FBSD LAN NIC directly to the other PC's NIC with a special crossover cable.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Installing the LAN ==&lt;br /&gt;
&lt;br /&gt;
Your PC should have two NIC already installed. &lt;br /&gt;
During the boot of FBSD review the /var/run/dmesg.boot log to verify that your PCI NIC were found. &lt;br /&gt;
&lt;br /&gt;
This is what you are looking for. This means that FBSD GENERIC kernel found your NIC. The dc0 name will be different depending on the manufacture of your NIC.&lt;br /&gt;
&lt;br /&gt;
dc0: &amp;lt;Macronix 98715/98715A 10/100BaseTX&amp;gt; port 0xdc00-0xdcff mem &lt;br /&gt;
0xe3000000-0xe30000ff irq 3 at device 19.0 on pci0&lt;br /&gt;
dc0: Ethernet address: 00:80:c6:f2:2e:3b&lt;br /&gt;
miibus0: &amp;lt;MII bus&amp;gt; on dc0&amp;gt;&lt;br /&gt;
dcphy0: &amp;lt;Intel 21143 NWAY media interface&amp;gt; on miibus0&lt;br /&gt;
dcphy0: 10baseT, 10baseT-FDX, 100baseTX, 100baseTX-FDX, auto&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dc0 is the NIC FBSD internal interface name.&lt;br /&gt;
&lt;br /&gt;
The generic kernel contains device statements for most of the NIC currently on the market. If the /var/run/dmesg.boot log shows your NIC as&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
pci0: &amp;lt;unknown card&amp;gt; (vendor=0x1274, dev=0x5000) at 19.0&lt;br /&gt;
&lt;br /&gt;
or no message to indicate the probe of the BOIS found any PCI devices, then you may have an older BOIS on your PC which does not handle PCI cards very well. On older (IE: pre Y2K) PC BOIS, it's very common for the system probe process of the bio's to be unable to find one or more PCI cards. If this happens to you, you have to do some research to determine the problem.&lt;br /&gt;
&lt;br /&gt;
Try the pciconf -lv command to see if it gives you any useful info. Then review the GENERIC source at /usr/src/sys/i386/conf/GENERIC to see if it contains any device statement comments about your NIC based on the manufacturer or chips used. If you do find a device statement in the GENERIC source for your NIC, then add this statement to your kernel source and recompile your kernel.&lt;br /&gt;
&lt;br /&gt;
device pun&lt;br /&gt;
&lt;br /&gt;
This device has additional code to probe your system's BIOS using different methods which in most cases results in your PCI NIC being found.&lt;br /&gt;
&lt;br /&gt;
If the review of the GENERIC kernel source produces no results, them review the kernel source file named LINT at /usr/src/sys/i386/conf/ for comments that describe your NIC by manufacturer name or chips used. Copy the appropriate device statements to the GENERIC kernel source file and then follow the instructions at Kernel Customizing. You will have to create a custom kernel from the GENERIC source including the device statement from the LINT source.&lt;br /&gt;
&lt;br /&gt;
If you find no kernel device statements for your NIC, then it's not supported and you have to get one that is.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== LAN private IP address ==&lt;br /&gt;
&lt;br /&gt;
There are ranges of special IP addresses reserved for use on private LANs. These special IP address ranges are non-routable on the public Internet. They are listed in the /etc/hosts file.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP address ranges for&lt;br /&gt;
private networks which will never be connected to the Internet:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
10.0.0.0    - 10.255.255.255 &lt;br /&gt;
172.16.0.0  - 172.31.255.255&lt;br /&gt;
192.168.0.0 - 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
These can also be written as&lt;br /&gt;
10.0.0.0/8 &lt;br /&gt;
172.16.0.0/12 &lt;br /&gt;
192.168.0.0/16&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
To communicate with the LAN PC's the FBSD system needs to know what the IP address range of the PC's on the LAN is and the LAN PC's needs to be configured with LAN network information so they know how to perform their part in the communication process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are two ways to accomplish this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The manually way by hand, or&lt;br /&gt;
&lt;br /&gt;
The automatic way using the FBSD port application DHCP.&lt;br /&gt;
&lt;br /&gt;
== Manually Configuring the Gateway host ==&lt;br /&gt;
 &lt;br /&gt;
Before you can manually configure each PC on the LAN by hand, you first have to collect some information from your FBSD gateway box. It's assumed you already have your gateway PC connection to the public Internet already working. &lt;br /&gt;
&lt;br /&gt;
The configuration file /etc/resolv.conf is automatically populated with the IP address of your ISP's primary and secondary domain name servers every time you log in to your ISP. Write down these IP addresses you will need them to configure your LAN PCs.&lt;br /&gt;
&lt;br /&gt;
Now you decide on the private IP address range to use for your LAN. This guide uses a very small portion of the 10.0.0.0/8 range for the private Lan, which is 10.0.10.0/29. This gives 10.0.10.0 through 10.0.10.7. The usable portion of the range is 10.0.10.1 through 10.0.10.6, the 10.0.10.0 and 10.0.10.7 is the reserved pair for broadcasting. &lt;br /&gt;
&lt;br /&gt;
The IP address of the NIC in the FBSD gateway will be 10.0.10.2. The IP address of the first LAN PC to be manually configured will be 10.0.10.3.&lt;br /&gt;
&lt;br /&gt;
On the FBSD gateway system add these two statements to /etc/rc.conf to manually assign the FBSD LAN NIC an IP address and tell FBSD to act as a gateway for the LAN.&lt;br /&gt;
&lt;br /&gt;
ifconfig_dc0=&amp;quot;inet 10.0.10.2 netmask 255.255.255.248&amp;quot;&lt;br /&gt;
&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dc0 is the gateway interface name of the NIC the LAN is cabled to.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually configuring LAN FBSD PC ==&lt;br /&gt;
&lt;br /&gt;
For a FBSD workstation PC on the LAN, add these statements to /etc/rc.conf to manually assign the FBSD LAN NIC an IP address. Be sure to change dc0 to the interface name of the Nic card in the FBSD LAN PC.&lt;br /&gt;
&lt;br /&gt;
ifconfig_dc0=&amp;quot;inet 10.0.10.3 netmask 255.255.255.248&amp;quot;&lt;br /&gt;
&lt;br /&gt;
defaultrouter=&amp;quot;10.0.10.2&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Copy the FBSD gateway /etc/resolv.conf file to the FBSD LAN PC, replacing the one that's there. Or edit the FBSD LAN PC's /etc/resolv.conf so it's the same as the one from the FBSD gateway. Reboot system to enable your changes.&lt;br /&gt;
&lt;br /&gt;
To test, ping the gateway server:&lt;br /&gt;
&lt;br /&gt;
ping -c 4 10.0.10.2&lt;br /&gt;
&lt;br /&gt;
Then test DNS by pinging:&lt;br /&gt;
&lt;br /&gt;
ping -c 4 freebsd.org&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually config MS/Windows LAN PC ==&lt;br /&gt;
 &lt;br /&gt;
This procedure has been tested on MS/Windows 98, ME and XP.&lt;br /&gt;
&lt;br /&gt;
Click on start, settings, control panel, networking. In the window the installed network components are displayed. Scroll through them and click to highlight the TCP/IP line for the NIC you are going to use to connect this box to your LAN. When it's highlighted, the properties button below the window becomes enabled. Click on the properties button and a window pops up which is where you manually configure the NIC TCP/IP network settings.&lt;br /&gt;
&lt;br /&gt;
Under the IP address tab, click on specify IP address. For IP address enter 10.0.10.4&lt;br /&gt;
&lt;br /&gt;
Under the gateway tab, new gateway window enter the IP address of the FBSD gateway&lt;br /&gt;
&lt;br /&gt;
10.0.10.2 and click the add button.&lt;br /&gt;
&lt;br /&gt;
Under the DNS configuration tab, click on enable DNS. In the DNS server search order window enter the first of the two IP addresses you got from the FBSD gateway /etc/resolv.conf file. Click on the add button, then do same thing over again for the second IP address. When you're finished click on the OK button at the bottom of the pop up window, and click OK again. The system will reboot to activate your changes.&lt;br /&gt;
&lt;br /&gt;
To test, click on start, run.&lt;br /&gt;
&lt;br /&gt;
Enter C:\windows\command.com&lt;br /&gt;
&lt;br /&gt;
When a native DOS window opens, ping the gateway server: &lt;br /&gt;
&lt;br /&gt;
Ping 10.0.10.2&lt;br /&gt;
&lt;br /&gt;
Then test DNS:&lt;br /&gt;
&lt;br /&gt;
ping freebsd.org&lt;br /&gt;
&lt;br /&gt;
The DNS servers will convert this domain name to an IP address and then send four pings to it. When this has completed, enter exit to leave native DOS mode.&lt;br /&gt;
&lt;br /&gt;
For each additional FBSD or MS/Windows LAN PCs you want to add, just increment the last digit of the last assigned IP address by 1. You may have to keep a log book so you know what LAN IP addresses you have assigned. All LAN PCs connected to the FBSD gateway 10.0.10.2 NIC have to use the same IP address sub-net, (IE: 10.0.10.x) where in this example x can be 1 through 6.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP (Dynamic Host Configuration Protocol) ==&lt;br /&gt;
 &lt;br /&gt;
If you are following the 'incremental install method' recommended in this Installers Guide, you have now completed the basic install of the FBSD Gateway/Firewall server with attached LAN. Everything up to this point has been accomplished using the built in facilities available in the standard FBSD stable release. &lt;br /&gt;
&lt;br /&gt;
In the previous section you manually configured your LAN PC's by hand with the information they needed to communicate with the FBSD gateway. DHCP is used to automate and control the automatic assignment of private IP addresses to your LAN environment. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
What function does DHCP perform?&lt;br /&gt;
The Dynamic Host Configuration Protocol (DHCP) is most commonly used in the situation where a LAN (local area network) has too many PC workstations for the LAN administrator to manually configuration each workstation with the information it needs to use for access on the LAN. To automate this process, DHCP was developed. DHCP usually runs on the gateway/firewall machine in server mode. It broadcasts its presence through the LAN to all the workstations who have a DHCP client version of DHCP installed. At workstation boot up it asks the DHCP server for the information necessary to configure itself for access to LAN services. &lt;br /&gt;
&lt;br /&gt;
All Microsoft Windows machines have a DHCP client built in that defaults to using DHCP services without any user configuration. FBSD also has a built in DHCP client, but it needs manual user input to activate it. Many ISP's use DHCP on dial up, DSL, and cable access to achieve the same results a LAN administrator wants for his private LAN.&lt;br /&gt;
&lt;br /&gt;
One of DHCP's major strengths is its ability to manage the dynamic assignment of IP addresses from a pool and to reuse any IP address released when a workstation is removed from the LAN or moved to a different location on the LAN, such as what normally happens in a company work place environment. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP Server ==&lt;br /&gt;
 &lt;br /&gt;
To add a DHCP server to FBSD you have to install the port. The best and most commonly used port for this purpose is the isc-dhcpd3 port. &lt;br /&gt;
The ISC-DHCP3 server supports three mechanisms for IP address allocation. In &amp;quot;automatic allocation&amp;quot;, DHCP assigns a permanent IP address to a client. In &amp;quot;dynamic allocation&amp;quot;, DHCP assigns an IP address to a client for a limited period of time (or until the client explicitly relinquishes the address). In &amp;quot;manual allocation&amp;quot;, a client's IP address is assigned by the network administrator, and DHCP is used simply to convey the assigned address to the client. Dynamic allocation is the only one of the three mechanisms that allows automatic reuse of am address that is no longer needed by the client to which it was assigned. A particular network will use one or more of these mechanisms, depending on the policies of the network administrator.&lt;br /&gt;
&lt;br /&gt;
For our purpose of a simple DHCP server that would fill the needs of the common FBSD user we are going to configure the DHCP server for &amp;quot;dynamic allocation&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How DHCP Works ==&lt;br /&gt;
&lt;br /&gt;
When the dhcpd daemon starts up at FBSD boot time, it broadcasts its presence through the LAN, then it sleeps and listens for broadcast requests for network configuration information from the LAN workstations. By default, it will listen on UDP port 67. When such a request is received, then the server will reply to the client machine on UDP port 68, providing the details required to connect to the network such as the IP address assigned to the workstation, subnet mask, default gateway and DNS servers names or IP addresses. Also included with this reply is a length of time for which this information can be used by that particular client. This is known as a DHCP &amp;quot;lease&amp;quot; and a new lease must be acquired by the client when it expires. The length of time for which a lease is valid is decided by the administrator of the DHCP server. The DHCP server keeps a database of leases it has issued in /var/db/dhcpd.leases File. This file is written as a log and can be edited. See man dhcpd.leases which gives a slightly longer description. DHCP clients can obtain a great deal of information from the server. An exhaustive list may be found in man dhcp-options &amp;amp; man dhcpd after DHCP is installed.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP Configuration Instructions ==&lt;br /&gt;
&lt;br /&gt;
To install the DHCP software, use the FBSD dhcp package using the following command &lt;br /&gt;
&lt;br /&gt;
pkg_add -rv isc-dhcp3-server&lt;br /&gt;
&lt;br /&gt;
To start the DHCPD server at boot time add the following statements in the /etc/rc.conf file.&lt;br /&gt;
&lt;br /&gt;
ee /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dhcpd_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
dhcpd_conf=&amp;quot;/usr/local/etc/dhcpd.conf&amp;quot;&lt;br /&gt;
dhcpd_ifaces=&amp;quot;xl0&amp;quot;&lt;br /&gt;
dhcpd_flags=&amp;quot;-q&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The -q option will turn off the copyright banner that displays during the FBSD boot up and in the DHCP log every time a broadcast is issued by the DHCP daemon or when a request is received from a workstation DHCP client.&lt;br /&gt;
&lt;br /&gt;
The dc0 is to be replaced with the interface name of the LAN NIC you want DHCP service on from your gateway/firewall FBSD system. &lt;br /&gt;
&lt;br /&gt;
The dhcpd.conf file is delivered as a sample file so you have to make a copy of it without its sample suffix. It contains a lot of comments and commented out statement examples which you can comment out or delete. Edit the main DHCP configuration file and make it look like this.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
cp dhcpd.conf.sample dhcpd.conf&lt;br /&gt;
&lt;br /&gt;
ee dhcpd.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
option domain-name &amp;quot;fbsdjones.com&amp;quot;;&lt;br /&gt;
option domain-name-servers 208.206.15.11, 208.206.15.12;&lt;br /&gt;
# 600=10min, 7200=2 hours, 86400=1 day, 604800=1 week, 2592000=30 days&lt;br /&gt;
default-lease-time 86400;&lt;br /&gt;
max-lease-time 604800;&lt;br /&gt;
authoritative;&lt;br /&gt;
ddns-update-style none;&lt;br /&gt;
log-facility local1;&lt;br /&gt;
# No service will be given on this subnet, but declaring it helps the &lt;br /&gt;
# DHCP server to understand the network topology.&lt;br /&gt;
subnet 10.152.187.0 netmask 255.255.255.0 { }&lt;br /&gt;
&lt;br /&gt;
# This is the fbsdjones.com subnet declaration.&lt;br /&gt;
# Max of 6 pc on LAN 10.0.10.1 - 10.0.10.6&lt;br /&gt;
# 10.0.10.2 is the IP address of the Nic card in FBSD&lt;br /&gt;
# 10.0.10.7 is the broadcast IP address &lt;br /&gt;
subnet 10.0.10.0 netmask 255.255.255.248 {&lt;br /&gt;
range 10.0.10.1 10.0.10.6;&lt;br /&gt;
option routers 10.0.10.2;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option domain-name &amp;quot;fbsdjones.com&amp;quot;; is the user selected domain name from the hostname=&amp;quot;gateway.fbsdjones.com&amp;quot; statement of /etc/rc.conf. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option domain-name-servers contains the DSN server's IP addresses of your ISP from /etc/resolv.conf nameserver statements which get populated automatically when you connect to your ISP. If you have your own private LAN domain DSN server, make it the first one in the list, and in that case you can use full domain names instead of IP address (such as dnslocal.fbsdjones.com, dsn1.isp-domain.com).&lt;br /&gt;
&lt;br /&gt;
The default-lease-time and max-lease-time have values in seconds to set the elapse period for these function. The values I show are good to go with.&lt;br /&gt;
&lt;br /&gt;
The authoritative; options tells the DHCP daemon server that it is the boss and is in control of issuing all the information to the LAN DHCP clients.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The ddns-update-style none; tells DHCP that there is no local LAN DSN server. If you have one, change this from none to interim. In the dhcpd.conf.sample you will see comments saying none and ad-hoc are the two options. This is no longer true for DHCP version 3.0. Ad-hoc has been deactivated and replaced with interim. See man dhcpd.conf for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The log-facility allows you to segregate the DHCP messages to a separate log for recording. You are going to use local1 for logging of DHCP server error messages;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
subnet 10.0.10.0 netmask 255.255.255.248 {&lt;br /&gt;
range 10.0.10.1 10.0.10.6;&lt;br /&gt;
option routers 10.0.10.2; }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The subnet 10.0.10.0 netmask 255.255.255.248 statement declares the maximum subnet IP address range. In this case the last three digits in the netmask, 248 determines the range. This means a total of 8 IP addresses, 10.0.10.0 through 10.0.10.7 are allocated as the subnet range. 10.0.10.0 and 10.0.10.7 are reserved for the broadcast process. &lt;br /&gt;
&lt;br /&gt;
The range 10.0.10.1 10.0.10.6; is saying this range of IP addresses makes up the pool of addresses that are to be used for dynamic IP allocation to DHCP clients. It's a small home LAN with only two MS/Windows boxes and a single FBSD box on it now. That can grow to six machines without making any changes to this statement group. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option routers 10.0.10.2 statement is a bit miss-leading. What this is referring to is the NIC in the FBSD box the DHCP server runs on and the LAN being configured is cabled to. In our case the NIC has an IP address of 10.0.10.2 which is specified in /etc/rc.conf by the ifconfig_dc0=&amp;quot;inet 10.0.10.2 netmask 255.255.255.248&amp;quot; statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The principle behind bitmasks and netmasks is simple, but often confusing to new users as it requires knowledge of binary numbers. For a quick reference, the following table illustrates what network ranges are indicated by the corresponding bitmasks/netmasks up to a default class C netmask. &lt;br /&gt;
&lt;br /&gt;
Bitmask   Netmask          Total IP's /  Usable IP's&lt;br /&gt;
  32      255.255.255.255         1              1&lt;br /&gt;
  31      255.255.255.254         2              1&lt;br /&gt;
  30      255.255.255.252         4              2&lt;br /&gt;
  29      255.255.255.248         8              6&lt;br /&gt;
  28      255.255.255.240        16             14&lt;br /&gt;
  27      255.255.255.224        32             30&lt;br /&gt;
  26      255.255.255.192        64             62&lt;br /&gt;
  25      255.255.255.128       128            126&lt;br /&gt;
  24      255.255.255.0         256            254&lt;br /&gt;
  22      255.255.192.0       16320          16318&lt;br /&gt;
  20      255.255.128.0       32768          32766&lt;br /&gt;
  16      255.255.0.0         65536          65534&lt;br /&gt;
  12      255.128.0.0   8.388608+e6    8.388606+e6 &lt;br /&gt;
   8      255.0.0.0           256^3      (256^3)-2&lt;br /&gt;
   0      0.0.0.0  (all IP's) 256^4       (256^4)-2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, there is a definite pattern. The number of total IP's always doubles, and the number of usable IP's is always total - 2. This is because for every IP network/subnet there are two IP's reserved for the network and broadcast addresses. The netmask's last octet starts at 255 and constantly decreases by multiples of 2, while the bitmask decreases by multiples of 1, because in binary, each shift over to the left halves the number, not divides by ten like in the decimal number system. This same pattern goes for all possible netmasks and bitmasks. &lt;br /&gt;
&lt;br /&gt;
Since you told DHCPD to use local1 for logging in the dhcpd.conf configuration file above, you now have to complete the logging environment configuration by adding the following statement to /etc/syslog.conf. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
ee /etc/syslog.conf &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local1.notice         /var/log/dhcpd.log&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
This log file does not exist, so you must create it.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/dhcpd.log&lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Now you must set up log rotation. Add this statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ee /etc/newsyslog.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/var/log/dhcp.log          600 3 100 * B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can change the log rotation triggers to whatever you want.&lt;br /&gt;
See man newsyslog for info on what the trigger values mean.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The DHCPD daemon has a start up script located at /usr/local/etc/rc.d/&lt;br /&gt;
&lt;br /&gt;
This directory location is where FBSD looks for files that end in .sh and executes them at the end of the boot process to start the applications. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can administer the DHCPD server from the command line using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/usr/local/etc/rc.d/isc-dhcp.sh start &lt;br /&gt;
                                stop&lt;br /&gt;
                                restart&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
                                &lt;br /&gt;
Restart is used to reread dhcp.conf file after making changes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now manually start DHCP by entering this on the command.&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/isc-dhcp.sh start &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Issue 'ps ax' command to see the DHCP daemon running in the active task list. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Testing the DHCPD Daemon ==&lt;br /&gt;
 &lt;br /&gt;
To test the DHCPD server you need a PC on the LAN. &lt;br /&gt;
&lt;br /&gt;
First let's check the LAN MS/Windows box network configuration. Click on the following buttons in this order. Start/settings/control panel/network/. Highlight TCP/IP and click on properties button. In the IP address tab the 'obtain IP address automatically' should be to only thing check marked. All the fields in the other tabs must be blank. If this is what you have use the cancel buttons to back yourself out. If you answer ok, you may have to have the windows install CDROM to update the network section.&lt;br /&gt;
&lt;br /&gt;
Windows 98, 2000, ME and XP have a program c:/windows/winipcfg.exe which will show you the DHCP info it's using. Start the winipcfg program by clicking on start, run, and type c:/windows/winipcfg.exe into the run window and then hit the OK button. Click on the more info button to see everything. You should be able to comprehend what you see back to the dhcpd.conf options as explained above. Click on the 'renew all' button to acquire a new DHCP lease.&lt;br /&gt;
&lt;br /&gt;
== FBSD as a DHCP Client ==&lt;br /&gt;
 &lt;br /&gt;
The isc-dhcp3 port comes with a client. I am not going to cover the isc-dhcp3 port client configuration process, because FBSD comes with a DHCP client built into the basic FBSD system. &lt;br /&gt;
&lt;br /&gt;
To activate the built in dhcp client on a FBSD LAN PC, edit /etc/rc.conf and add the following statement to tell FBSD what interface the client DHCP should use:&lt;br /&gt;
&lt;br /&gt;
ee /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ifconfig_dc0=&amp;quot;DHCP&amp;quot;     # Where dc0 is the FBSD Nic card interface name.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That's it, configuration complete. Reboot to activate your changes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Local_Area_Network_(LAN)</id>
		<title>Local Area Network (LAN)</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Local_Area_Network_(LAN)"/>
				<updated>2012-08-14T00:04:00Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* DHCP Configuration Instructions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Setting up Local Area Network (LAN) ==&lt;br /&gt;
&lt;br /&gt;
A local area network (LAN) is a group of computers and associated devices that share a common communications line or wireless link and typically share the resources of a single processor or server within a small geographic area (for example, within an office building). Usually, the server has applications and data storage that are shared in common by multiple LAN computer users. A local area network may serve as few as two or three users (for example, in a home network) or as many as thousands of users (for example, in an FDDI network). Typically, a suite of application programs can be kept on the LAN server. Users who need an application frequently can download it once and then run it from their local hard disk. A user can share files with others at the LAN server. &lt;br /&gt;
&lt;br /&gt;
There are many technical limitations and options to how a LAN is configured depending on if you are a non-commercial user or commercial user and how many of the MS/Windows and/or Unix network server sharing facilities you want to enable. &lt;br /&gt;
&lt;br /&gt;
Basically what determines if you are commercial user or not is how you are known to the public Internet. A commercial user has a permanent, dedicated, high-speed leased Internet line connecting them to their ISP and one or more static IP addresses assigned by their ISP. A static IP address is always the same number; it never changes between logins to the ISP. They have an official registered domain name that points to one of the static IP address which points to their PC that is acting as their gateway. If the commercial user pays for a large block of static IP addresses then they can use these IP address for the computers on the LAN and not need to use NAT (network address translation). Their email will arrive at the gateway PC and is processed by their mail server directly. They do not use their ISP to receive and hold their email for them.&lt;br /&gt;
&lt;br /&gt;
An non-commercial user, like the home user, uses a phone line dial in login to their ISP on a limited speed connection or has a 24/7 cable or DSL high speed connection and gets assigned a single dynamic IP address which changes every time they login. Their ISP receives and holds all their email for them. The only way a public Internet user can find them is if that know the dynamic IP address currently in use by them. From the ISP viewpoint a non-commercial user uses a very small amount of its overall resources and so charges much less for a single user account.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FBSD system that is acting as the gateway can also be configured to provide different levels of network sharing depending on what kind of operating systems are running on the PCs connected to the LAN. For Unix-like operating systems NFS provides network file and device sharing, while the FBSD port application Samba does the same thing for MS/Windows PCs on the LAN. These facilities, NFS and Samba are not covered in this guide as they are more applicable to commercial users who have large LANs.&lt;br /&gt;
&lt;br /&gt;
See this link for Samba details http://us1.samba.org/samba/samba.html&lt;br /&gt;
&lt;br /&gt;
See the following links for details on FBSD NFS:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/network-nfs.html&lt;br /&gt;
&lt;br /&gt;
http://www.onlamp.com/pub/a/bsd/2000/07/26/FreeBSD_Basics.html&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Home User LANs ==&lt;br /&gt;
&lt;br /&gt;
Normally each family member would have to have their own phone line and unique ISP account to connect all of the family PCs to the Internet simultaneously. This is a costly way of doing this. The alternative is to have a single FBSD system gateway connect to an ISP and then network the other family members PC's behind the gateway using private IP addresses and NAT (Network Addressing Translation) so everything leaving the gateway system looks like it came from the single dynamic IP address assigned by the ISP. Your ISP can not tell if the packet passing through them has been NATed or not.&lt;br /&gt;
&lt;br /&gt;
Installer Note: When you sign up for service with your ISP you have to sign a user agreement that basically says you are not allowed to do NAT on your PC or run email services or web servers. If you are caught, it's grounds for them to terminate your account. Never tell your ISP tech support people what you are doing. Most ISP's leave open all the ports except the port used by an email server, which they block. More recently some ISPs have started to also block the web server port number.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another simple to configure facility is an anonymous FTP server on the FBSD gateway so LAN users can post files there that they want to pass to other LAN users. This allows them to pass large files between LAN users.&lt;br /&gt;
&lt;br /&gt;
The LAN can be populated with both MS/Windows boxes and FBSD boxes and not cause any problems. ISP's usually allow 5 email address per dial in account. Each family member can have their own email address and using the email client on their PC get their email directly from the ISP email server. Or you can run a task on the FBSD gateway box to download the email from the ISP account on a recurring schedule and store it in the FBSD built in email server called sendmail, and then have all the LAN users get their email from the sendmail server without having the FBSD gateway connected to the Internet.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Topography of a LAN&lt;br /&gt;
        __________         ________             _____ &lt;br /&gt;
       |          |       |        |           |     | &lt;br /&gt;
       |  FBSD/GW |       | switch |&amp;lt;---------&amp;gt;| LAN |&lt;br /&gt;
       |          |       | or     |           | PC1 |    _____&lt;br /&gt;
       |      NIC |&amp;lt;-----&amp;gt;| hub    |&amp;lt;--|       |_____|   |     |&lt;br /&gt;
       |__________|       |________|   |                 | LAN |&lt;br /&gt;
                                       |&amp;lt;---------------&amp;gt;| PC2 |&lt;br /&gt;
                                                         |_____| &lt;br /&gt;
                                                             &lt;br /&gt;
                                                            &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The above diagram shows a simple single LAN circuit. Your FBSD gateway box needs a NIC for each separate LAN circuit. Each circuit must use a unique IP address subnet. You cable the LAN NIC from the back of the FBSD gateway PC to a network switch or hub. A small cheap switch normally has 5 plugs. One plug for each PC on the LAN including the FBSD gateway. You run a cable from the switch to the NIC of each PC you want on the LAN. A LAN circuit can handle many PC's and many downstream switches as long as the max distance of the cable is not exceeded. To add more LAN users you create another circuit by adding another NIC in the FBSD box connected to another switch which has more LAN PC's connected to it. Please note, this is a very simplified LAN description and layout, but is adequate for basic understanding of how the physical parts of the LAN are cabled together.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For the home user with just two PCs, you can cable your FBSD LAN NIC directly to the other PC's NIC with a special crossover cable.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Installing the LAN ==&lt;br /&gt;
&lt;br /&gt;
Your PC should have two NIC already installed. &lt;br /&gt;
During the boot of FBSD review the /var/run/dmesg.boot log to verify that your PCI NIC were found. &lt;br /&gt;
&lt;br /&gt;
This is what you are looking for. This means that FBSD GENERIC kernel found your NIC. The dc0 name will be different depending on the manufacture of your NIC.&lt;br /&gt;
&lt;br /&gt;
dc0: &amp;lt;Macronix 98715/98715A 10/100BaseTX&amp;gt; port 0xdc00-0xdcff mem &lt;br /&gt;
0xe3000000-0xe30000ff irq 3 at device 19.0 on pci0&lt;br /&gt;
dc0: Ethernet address: 00:80:c6:f2:2e:3b&lt;br /&gt;
miibus0: &amp;lt;MII bus&amp;gt; on dc0&amp;gt;&lt;br /&gt;
dcphy0: &amp;lt;Intel 21143 NWAY media interface&amp;gt; on miibus0&lt;br /&gt;
dcphy0: 10baseT, 10baseT-FDX, 100baseTX, 100baseTX-FDX, auto&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dc0 is the NIC FBSD internal interface name.&lt;br /&gt;
&lt;br /&gt;
The generic kernel contains device statements for most of the NIC currently on the market. If the /var/run/dmesg.boot log shows your NIC as&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
pci0: &amp;lt;unknown card&amp;gt; (vendor=0x1274, dev=0x5000) at 19.0&lt;br /&gt;
&lt;br /&gt;
or no message to indicate the probe of the BOIS found any PCI devices, then you may have an older BOIS on your PC which does not handle PCI cards very well. On older (IE: pre Y2K) PC BOIS, it's very common for the system probe process of the bio's to be unable to find one or more PCI cards. If this happens to you, you have to do some research to determine the problem.&lt;br /&gt;
&lt;br /&gt;
Try the pciconf -lv command to see if it gives you any useful info. Then review the GENERIC source at /usr/src/sys/i386/conf/GENERIC to see if it contains any device statement comments about your NIC based on the manufacturer or chips used. If you do find a device statement in the GENERIC source for your NIC, then add this statement to your kernel source and recompile your kernel.&lt;br /&gt;
&lt;br /&gt;
device pun&lt;br /&gt;
&lt;br /&gt;
This device has additional code to probe your system's BIOS using different methods which in most cases results in your PCI NIC being found.&lt;br /&gt;
&lt;br /&gt;
If the review of the GENERIC kernel source produces no results, them review the kernel source file named LINT at /usr/src/sys/i386/conf/ for comments that describe your NIC by manufacturer name or chips used. Copy the appropriate device statements to the GENERIC kernel source file and then follow the instructions at Kernel Customizing. You will have to create a custom kernel from the GENERIC source including the device statement from the LINT source.&lt;br /&gt;
&lt;br /&gt;
If you find no kernel device statements for your NIC, then it's not supported and you have to get one that is.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== LAN private IP address ==&lt;br /&gt;
&lt;br /&gt;
There are ranges of special IP addresses reserved for use on private LANs. These special IP address ranges are non-routable on the public Internet. They are listed in the /etc/hosts file.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP address ranges for&lt;br /&gt;
private networks which will never be connected to the Internet:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
10.0.0.0    - 10.255.255.255 &lt;br /&gt;
172.16.0.0  - 172.31.255.255&lt;br /&gt;
192.168.0.0 - 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
These can also be written as&lt;br /&gt;
10.0.0.0/8 &lt;br /&gt;
172.16.0.0/12 &lt;br /&gt;
192.168.0.0/16&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
To communicate with the LAN PC's the FBSD system needs to know what the IP address range of the PC's on the LAN is and the LAN PC's needs to be configured with LAN network information so they know how to perform their part in the communication process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are two ways to accomplish this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The manually way by hand, or&lt;br /&gt;
&lt;br /&gt;
The automatic way using the FBSD port application DHCP.&lt;br /&gt;
&lt;br /&gt;
== Manually Configuring the Gateway host ==&lt;br /&gt;
 &lt;br /&gt;
Before you can manually configure each PC on the LAN by hand, you first have to collect some information from your FBSD gateway box. It's assumed you already have your gateway PC connection to the public Internet already working. &lt;br /&gt;
&lt;br /&gt;
The configuration file /etc/resolv.conf is automatically populated with the IP address of your ISP's primary and secondary domain name servers every time you log in to your ISP. Write down these IP addresses you will need them to configure your LAN PCs.&lt;br /&gt;
&lt;br /&gt;
Now you decide on the private IP address range to use for your LAN. This guide uses a very small portion of the 10.0.0.0/8 range for the private Lan, which is 10.0.10.0/29. This gives 10.0.10.0 through 10.0.10.7. The usable portion of the range is 10.0.10.1 through 10.0.10.6, the 10.0.10.0 and 10.0.10.7 is the reserved pair for broadcasting. &lt;br /&gt;
&lt;br /&gt;
The IP address of the NIC in the FBSD gateway will be 10.0.10.2. The IP address of the first LAN PC to be manually configured will be 10.0.10.3.&lt;br /&gt;
&lt;br /&gt;
On the FBSD gateway system add these two statements to /etc/rc.conf to manually assign the FBSD LAN NIC an IP address and tell FBSD to act as a gateway for the LAN.&lt;br /&gt;
&lt;br /&gt;
ifconfig_dc0=&amp;quot;inet 10.0.10.2 netmask 255.255.255.248&amp;quot;&lt;br /&gt;
&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dc0 is the gateway interface name of the NIC the LAN is cabled to.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually configuring LAN FBSD PC ==&lt;br /&gt;
&lt;br /&gt;
For a FBSD workstation PC on the LAN, add these statements to /etc/rc.conf to manually assign the FBSD LAN NIC an IP address. Be sure to change dc0 to the interface name of the Nic card in the FBSD LAN PC.&lt;br /&gt;
&lt;br /&gt;
ifconfig_dc0=&amp;quot;inet 10.0.10.3 netmask 255.255.255.248&amp;quot;&lt;br /&gt;
&lt;br /&gt;
defaultrouter=&amp;quot;10.0.10.2&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Copy the FBSD gateway /etc/resolv.conf file to the FBSD LAN PC, replacing the one that's there. Or edit the FBSD LAN PC's /etc/resolv.conf so it's the same as the one from the FBSD gateway. Reboot system to enable your changes.&lt;br /&gt;
&lt;br /&gt;
To test, ping the gateway server:&lt;br /&gt;
&lt;br /&gt;
ping -c 4 10.0.10.2&lt;br /&gt;
&lt;br /&gt;
Then test DNS by pinging:&lt;br /&gt;
&lt;br /&gt;
ping -c 4 freebsd.org&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually config MS/Windows LAN PC ==&lt;br /&gt;
 &lt;br /&gt;
This procedure has been tested on MS/Windows 98, ME and XP.&lt;br /&gt;
&lt;br /&gt;
Click on start, settings, control panel, networking. In the window the installed network components are displayed. Scroll through them and click to highlight the TCP/IP line for the NIC you are going to use to connect this box to your LAN. When it's highlighted, the properties button below the window becomes enabled. Click on the properties button and a window pops up which is where you manually configure the NIC TCP/IP network settings.&lt;br /&gt;
&lt;br /&gt;
Under the IP address tab, click on specify IP address. For IP address enter 10.0.10.4&lt;br /&gt;
&lt;br /&gt;
Under the gateway tab, new gateway window enter the IP address of the FBSD gateway&lt;br /&gt;
&lt;br /&gt;
10.0.10.2 and click the add button.&lt;br /&gt;
&lt;br /&gt;
Under the DNS configuration tab, click on enable DNS. In the DNS server search order window enter the first of the two IP addresses you got from the FBSD gateway /etc/resolv.conf file. Click on the add button, then do same thing over again for the second IP address. When you're finished click on the OK button at the bottom of the pop up window, and click OK again. The system will reboot to activate your changes.&lt;br /&gt;
&lt;br /&gt;
To test, click on start, run.&lt;br /&gt;
&lt;br /&gt;
Enter C:\windows\command.com&lt;br /&gt;
&lt;br /&gt;
When a native DOS window opens, ping the gateway server: &lt;br /&gt;
&lt;br /&gt;
Ping 10.0.10.2&lt;br /&gt;
&lt;br /&gt;
Then test DNS:&lt;br /&gt;
&lt;br /&gt;
ping freebsd.org&lt;br /&gt;
&lt;br /&gt;
The DNS servers will convert this domain name to an IP address and then send four pings to it. When this has completed, enter exit to leave native DOS mode.&lt;br /&gt;
&lt;br /&gt;
For each additional FBSD or MS/Windows LAN PCs you want to add, just increment the last digit of the last assigned IP address by 1. You may have to keep a log book so you know what LAN IP addresses you have assigned. All LAN PCs connected to the FBSD gateway 10.0.10.2 NIC have to use the same IP address sub-net, (IE: 10.0.10.x) where in this example x can be 1 through 6.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP (Dynamic Host Configuration Protocol) ==&lt;br /&gt;
 &lt;br /&gt;
If you are following the 'incremental install method' recommended in this Installers Guide, you have now completed the basic install of the FBSD Gateway/Firewall server with attached LAN. Everything up to this point has been accomplished using the built in facilities available in the standard FBSD stable release. &lt;br /&gt;
&lt;br /&gt;
In the previous section you manually configured your LAN PC's by hand with the information they needed to communicate with the FBSD gateway. DHCP is used to automate and control the automatic assignment of private IP addresses to your LAN environment. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
What function does DHCP perform?&lt;br /&gt;
The Dynamic Host Configuration Protocol (DHCP) is most commonly used in the situation where a LAN (local area network) has too many PC workstations for the LAN administrator to manually configuration each workstation with the information it needs to use for access on the LAN. To automate this process, DHCP was developed. DHCP usually runs on the gateway/firewall machine in server mode. It broadcasts its presence through the LAN to all the workstations who have a DHCP client version of DHCP installed. At workstation boot up it asks the DHCP server for the information necessary to configure itself for access to LAN services. &lt;br /&gt;
&lt;br /&gt;
All Microsoft Windows machines have a DHCP client built in that defaults to using DHCP services without any user configuration. FBSD also has a built in DHCP client, but it needs manual user input to activate it. Many ISP's use DHCP on dial up, DSL, and cable access to achieve the same results a LAN administrator wants for his private LAN.&lt;br /&gt;
&lt;br /&gt;
One of DHCP's major strengths is its ability to manage the dynamic assignment of IP addresses from a pool and to reuse any IP address released when a workstation is removed from the LAN or moved to a different location on the LAN, such as what normally happens in a company work place environment. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP Server ==&lt;br /&gt;
 &lt;br /&gt;
To add a DHCP server to FBSD you have to install the port. The best and most commonly used port for this purpose is the isc-dhcpd3 port. &lt;br /&gt;
The ISC-DHCP3 server supports three mechanisms for IP address allocation. In &amp;quot;automatic allocation&amp;quot;, DHCP assigns a permanent IP address to a client. In &amp;quot;dynamic allocation&amp;quot;, DHCP assigns an IP address to a client for a limited period of time (or until the client explicitly relinquishes the address). In &amp;quot;manual allocation&amp;quot;, a client's IP address is assigned by the network administrator, and DHCP is used simply to convey the assigned address to the client. Dynamic allocation is the only one of the three mechanisms that allows automatic reuse of am address that is no longer needed by the client to which it was assigned. A particular network will use one or more of these mechanisms, depending on the policies of the network administrator.&lt;br /&gt;
&lt;br /&gt;
For our purpose of a simple DHCP server that would fill the needs of the common FBSD user we are going to configure the DHCP server for &amp;quot;dynamic allocation&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How DHCP Works ==&lt;br /&gt;
&lt;br /&gt;
When the dhcpd daemon starts up at FBSD boot time, it broadcasts its presence through the LAN, then it sleeps and listens for broadcast requests for network configuration information from the LAN workstations. By default, it will listen on UDP port 67. When such a request is received, then the server will reply to the client machine on UDP port 68, providing the details required to connect to the network such as the IP address assigned to the workstation, subnet mask, default gateway and DNS servers names or IP addresses. Also included with this reply is a length of time for which this information can be used by that particular client. This is known as a DHCP &amp;quot;lease&amp;quot; and a new lease must be acquired by the client when it expires. The length of time for which a lease is valid is decided by the administrator of the DHCP server. The DHCP server keeps a database of leases it has issued in /var/db/dhcpd.leases File. This file is written as a log and can be edited. See man dhcpd.leases which gives a slightly longer description. DHCP clients can obtain a great deal of information from the server. An exhaustive list may be found in man dhcp-options &amp;amp; man dhcpd after DHCP is installed.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP Configuration Instructions ==&lt;br /&gt;
&lt;br /&gt;
To install the DHCP software, use the FBSD dhcp package using the following command &lt;br /&gt;
&lt;br /&gt;
pkg_add -rv isc-dhcp3-server&lt;br /&gt;
&lt;br /&gt;
To start the DHCPD server at boot time add the following statements in the /etc/rc.conf file.&lt;br /&gt;
&lt;br /&gt;
ee /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dhcpd_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
dhcpd_conf=&amp;quot;/usr/local/etc/dhcpd.conf&amp;quot;&lt;br /&gt;
dhcpd_ifaces=&amp;quot;xl0&amp;quot;&lt;br /&gt;
dhcpd_flags=&amp;quot;-q&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The -q option will turn off the copyright banner that displays during the FBSD boot up and in the DHCP log every time a broadcast is issued by the DHCP daemon or when a request is received from a workstation DHCP client.&lt;br /&gt;
&lt;br /&gt;
The dc0 is to be replaced with the interface name of the LAN NIC you want DHCP service on from your gateway/firewall FBSD system. &lt;br /&gt;
&lt;br /&gt;
The dhcpd.conf file is delivered as a sample file so you have to make a copy of it without its sample suffix. It contains a lot of comments and commented out statement examples which you can comment out or delete. Edit the main DHCP configuration file and make it look like this.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
cp dhcpd.conf.sample dhcpd.conf&lt;br /&gt;
&lt;br /&gt;
ee dhcpd.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
option domain-name &amp;quot;fbsdjones.com&amp;quot;;&lt;br /&gt;
option domain-name-servers 208.206.15.11, 208.206.15.12;&lt;br /&gt;
# 600=10min, 7200=2 hours, 86400=1 day, 604800=1 week, 2592000=30 days&lt;br /&gt;
default-lease-time 86400;&lt;br /&gt;
max-lease-time 604800;&lt;br /&gt;
authoritative;&lt;br /&gt;
ddns-update-style none;&lt;br /&gt;
log-facility local1;&lt;br /&gt;
# No service will be given on this subnet, but declaring it helps the &lt;br /&gt;
# DHCP server to understand the network topology.&lt;br /&gt;
subnet 10.152.187.0 netmask 255.255.255.0 { }&lt;br /&gt;
&lt;br /&gt;
# This is the fbsdjones.com subnet declaration.&lt;br /&gt;
# Max of 6 pc on LAN 10.0.10.1 - 10.0.10.6&lt;br /&gt;
# 10.0.10.2 is the IP address of the Nic card in FBSD&lt;br /&gt;
# 10.0.10.7 is the broadcast IP address &lt;br /&gt;
subnet 10.0.10.0 netmask 255.255.255.248 {&lt;br /&gt;
range 10.0.10.1 10.0.10.6;&lt;br /&gt;
option routers 10.0.10.2;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option domain-name &amp;quot;fbsdjones.com&amp;quot;; is the user selected domain name from the hostname=&amp;quot;gateway.fbsdjones.com&amp;quot; statement of /etc/rc.conf. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option domain-name-servers contains the DSN server's IP addresses of your ISP from /etc/resolv.conf nameserver statements which get populated automatically when you connect to your ISP. If you have your own private LAN domain DSN server, make it the first one in the list, and in that case you can use full domain names instead of IP address (such as dnslocal.fbsdjones.com, dsn1.isp-domain.com).&lt;br /&gt;
&lt;br /&gt;
The default-lease-time and max-lease-time have values in seconds to set the elapse period for these function. The values I show are good to go with.&lt;br /&gt;
&lt;br /&gt;
The authoritative; options tells the DHCP daemon server that it is the boss and is in control of issuing all the information to the LAN DHCP clients.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The ddns-update-style none; tells DHCP that there is no local LAN DSN server. If you have one, change this from none to interim. In the dhcpd.conf.sample you will see comments saying none and ad-hoc are the two options. This is no longer true for DHCP version 3.0. Ad-hoc has been deactivated and replaced with interim. See man dhcpd.conf for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The log-facility allows you to segregate the DHCP messages to a separate log for recording. You are going to use local1 for logging of DHCP server error messages;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
subnet 10.0.10.0 netmask 255.255.255.248 {&lt;br /&gt;
range 10.0.10.1 10.0.10.6;&lt;br /&gt;
option routers 10.0.10.2; }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The subnet 10.0.10.0 netmask 255.255.255.248 statement declares the maximum subnet IP address range. In this case the last three digits in the netmask, 248 determines the range. This means a total of 8 IP addresses, 10.0.10.0 through 10.0.10.7 are allocated as the subnet range. 10.0.10.0 and 10.0.10.7 are reserved for the broadcast process. &lt;br /&gt;
&lt;br /&gt;
The range 10.0.10.1 10.0.10.6; is saying this range of IP addresses makes up the pool of addresses that are to be used for dynamic IP allocation to DHCP clients. It's a small home LAN with only two MS/Windows boxes and a single FBSD box on it now. That can grow to six machines without making any changes to this statement group. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option routers 10.0.10.2 statement is a bit miss-leading. What this is referring to is the NIC in the FBSD box the DHCP server runs on and the LAN being configured is cabled to. In our case the NIC has an IP address of 10.0.10.2 which is specified in /etc/rc.conf by the ifconfig_dc0=&amp;quot;inet 10.0.10.2 netmask 255.255.255.248&amp;quot; statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The principle behind bitmasks and netmasks is simple, but often confusing to new users as it requires knowledge of binary numbers. For a quick reference, the following table illustrates what network ranges are indicated by the corresponding bitmasks/netmasks up to a default class C netmask. &lt;br /&gt;
&lt;br /&gt;
Bitmask   Netmask          Total IP's /  Usable IP's&lt;br /&gt;
  32      255.255.255.255         1              1&lt;br /&gt;
  31      255.255.255.254         2              1&lt;br /&gt;
  30      255.255.255.252         4              2&lt;br /&gt;
  29      255.255.255.248         8              6&lt;br /&gt;
  28      255.255.255.240        16             14&lt;br /&gt;
  27      255.255.255.224        32             30&lt;br /&gt;
  26      255.255.255.192        64             62&lt;br /&gt;
  25      255.255.255.128       128            126&lt;br /&gt;
  24      255.255.255.0         256            254&lt;br /&gt;
  22      255.255.192.0       16320          16318&lt;br /&gt;
  20      255.255.128.0       32768          32766&lt;br /&gt;
  16      255.255.0.0         65536          65534&lt;br /&gt;
  12      255.128.0.0   8.388608+e6    8.388606+e6 &lt;br /&gt;
   8      255.0.0.0           256^3      (256^3)-2&lt;br /&gt;
   0      0.0.0.0  (all IP's) 256^4       (256^4)-2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, there is a definite pattern. The number of total IP's always doubles, and the number of usable IP's is always total - 2. This is because for every IP network/subnet there are two IP's reserved for the network and broadcast addresses. The netmask's last octet starts at 255 and constantly decreases by multiples of 2, while the bitmask decreases by multiples of 1, because in binary, each shift over to the left halves the number, not divides by ten like in the decimal number system. This same pattern goes for all possible netmasks and bitmasks. &lt;br /&gt;
&lt;br /&gt;
Since you told DHCPD to use local1 for logging in the dhcpd.conf configuration file above, you now have to complete the logging environment configuration by adding the following statement to /etc/syslog.conf. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
ee /etc/syslog.conf &lt;br /&gt;
&lt;br /&gt;
local1.notice         /var/log/dhcpd.log&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
This log file does not exist, so you must create it.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/dhcpd.log&lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Now you must set up log rotation. Add this statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ee /etc/newsyslog.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/var/log/dhcp.log          600 3 100 * B&lt;br /&gt;
&lt;br /&gt;
You can change the log rotation triggers to whatever you want.&lt;br /&gt;
See man newsyslog for info on what the trigger values mean.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The DHCPD daemon has a start up script located at /usr/local/etc/rc.d/&lt;br /&gt;
&lt;br /&gt;
This directory location is where FBSD looks for files that end in .sh and executes them at the end of the boot process to start the applications. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can administer the DHCPD server from the command line using&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/isc-dhcp.sh start &lt;br /&gt;
                                stop&lt;br /&gt;
                                restart&lt;br /&gt;
                                &lt;br /&gt;
Restart is used to reread dhcp.conf file after making changes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now manually start DHCP by entering this on the command.&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/isc-dhcp.sh start &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Issue 'ps ax' command to see the DHCP daemon running in the active task list. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Testing the DHCPD Daemon &lt;br /&gt;
To test the DHCPD server you need a PC on the LAN. &lt;br /&gt;
&lt;br /&gt;
First let's check the LAN MS/Windows box network configuration. Click on the following buttons in this order. Start/settings/control panel/network/. Highlight TCP/IP and click on properties button. In the IP address tab the 'obtain IP address automatically' should be to only thing check marked. All the fields in the other tabs must be blank. If this is what you have use the cancel buttons to back yourself out. If you answer ok, you may have to have the windows install CDROM to update the network section.&lt;br /&gt;
&lt;br /&gt;
Windows 98, 2000, and ME have a program c:/windows/winipcfg.exe which will show you the DHCP info it's using. Start the winipcfg program by clicking on start, run, and type c:/windows/winipcfg.exe into the run window and then hit the OK button. Click on the more info button to see everything. You should be able to comprehend what you see back to the dhcpd.conf options as explained above. Click on the 'renew all' button to acquire a new DHCP lease.&lt;br /&gt;
&lt;br /&gt;
== FBSD as a DHCP Client ==&lt;br /&gt;
 &lt;br /&gt;
The isc-dhcp3 port comes with a client. I am not going to cover the isc-dhcp3 port client configuration process, because FBSD comes with a DHCP client built into the basic FBSD system. &lt;br /&gt;
&lt;br /&gt;
To activate the built in dhcp client on a FBSD LAN PC, edit /etc/rc.conf and add the following statement to tell FBSD what interface the client DHCP should use:&lt;br /&gt;
&lt;br /&gt;
ee /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ifconfig_dc0=&amp;quot;DHCP&amp;quot;     # Where dc0 is the FBSD Nic card interface name.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That's it, configuration complete. Reboot to activate your changes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Local_Area_Network_(LAN)</id>
		<title>Local Area Network (LAN)</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Local_Area_Network_(LAN)"/>
				<updated>2012-08-14T00:01:48Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* LAN private IP address */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Setting up Local Area Network (LAN) ==&lt;br /&gt;
&lt;br /&gt;
A local area network (LAN) is a group of computers and associated devices that share a common communications line or wireless link and typically share the resources of a single processor or server within a small geographic area (for example, within an office building). Usually, the server has applications and data storage that are shared in common by multiple LAN computer users. A local area network may serve as few as two or three users (for example, in a home network) or as many as thousands of users (for example, in an FDDI network). Typically, a suite of application programs can be kept on the LAN server. Users who need an application frequently can download it once and then run it from their local hard disk. A user can share files with others at the LAN server. &lt;br /&gt;
&lt;br /&gt;
There are many technical limitations and options to how a LAN is configured depending on if you are a non-commercial user or commercial user and how many of the MS/Windows and/or Unix network server sharing facilities you want to enable. &lt;br /&gt;
&lt;br /&gt;
Basically what determines if you are commercial user or not is how you are known to the public Internet. A commercial user has a permanent, dedicated, high-speed leased Internet line connecting them to their ISP and one or more static IP addresses assigned by their ISP. A static IP address is always the same number; it never changes between logins to the ISP. They have an official registered domain name that points to one of the static IP address which points to their PC that is acting as their gateway. If the commercial user pays for a large block of static IP addresses then they can use these IP address for the computers on the LAN and not need to use NAT (network address translation). Their email will arrive at the gateway PC and is processed by their mail server directly. They do not use their ISP to receive and hold their email for them.&lt;br /&gt;
&lt;br /&gt;
An non-commercial user, like the home user, uses a phone line dial in login to their ISP on a limited speed connection or has a 24/7 cable or DSL high speed connection and gets assigned a single dynamic IP address which changes every time they login. Their ISP receives and holds all their email for them. The only way a public Internet user can find them is if that know the dynamic IP address currently in use by them. From the ISP viewpoint a non-commercial user uses a very small amount of its overall resources and so charges much less for a single user account.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FBSD system that is acting as the gateway can also be configured to provide different levels of network sharing depending on what kind of operating systems are running on the PCs connected to the LAN. For Unix-like operating systems NFS provides network file and device sharing, while the FBSD port application Samba does the same thing for MS/Windows PCs on the LAN. These facilities, NFS and Samba are not covered in this guide as they are more applicable to commercial users who have large LANs.&lt;br /&gt;
&lt;br /&gt;
See this link for Samba details http://us1.samba.org/samba/samba.html&lt;br /&gt;
&lt;br /&gt;
See the following links for details on FBSD NFS:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/network-nfs.html&lt;br /&gt;
&lt;br /&gt;
http://www.onlamp.com/pub/a/bsd/2000/07/26/FreeBSD_Basics.html&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Home User LANs ==&lt;br /&gt;
&lt;br /&gt;
Normally each family member would have to have their own phone line and unique ISP account to connect all of the family PCs to the Internet simultaneously. This is a costly way of doing this. The alternative is to have a single FBSD system gateway connect to an ISP and then network the other family members PC's behind the gateway using private IP addresses and NAT (Network Addressing Translation) so everything leaving the gateway system looks like it came from the single dynamic IP address assigned by the ISP. Your ISP can not tell if the packet passing through them has been NATed or not.&lt;br /&gt;
&lt;br /&gt;
Installer Note: When you sign up for service with your ISP you have to sign a user agreement that basically says you are not allowed to do NAT on your PC or run email services or web servers. If you are caught, it's grounds for them to terminate your account. Never tell your ISP tech support people what you are doing. Most ISP's leave open all the ports except the port used by an email server, which they block. More recently some ISPs have started to also block the web server port number.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another simple to configure facility is an anonymous FTP server on the FBSD gateway so LAN users can post files there that they want to pass to other LAN users. This allows them to pass large files between LAN users.&lt;br /&gt;
&lt;br /&gt;
The LAN can be populated with both MS/Windows boxes and FBSD boxes and not cause any problems. ISP's usually allow 5 email address per dial in account. Each family member can have their own email address and using the email client on their PC get their email directly from the ISP email server. Or you can run a task on the FBSD gateway box to download the email from the ISP account on a recurring schedule and store it in the FBSD built in email server called sendmail, and then have all the LAN users get their email from the sendmail server without having the FBSD gateway connected to the Internet.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Topography of a LAN&lt;br /&gt;
        __________         ________             _____ &lt;br /&gt;
       |          |       |        |           |     | &lt;br /&gt;
       |  FBSD/GW |       | switch |&amp;lt;---------&amp;gt;| LAN |&lt;br /&gt;
       |          |       | or     |           | PC1 |    _____&lt;br /&gt;
       |      NIC |&amp;lt;-----&amp;gt;| hub    |&amp;lt;--|       |_____|   |     |&lt;br /&gt;
       |__________|       |________|   |                 | LAN |&lt;br /&gt;
                                       |&amp;lt;---------------&amp;gt;| PC2 |&lt;br /&gt;
                                                         |_____| &lt;br /&gt;
                                                             &lt;br /&gt;
                                                            &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The above diagram shows a simple single LAN circuit. Your FBSD gateway box needs a NIC for each separate LAN circuit. Each circuit must use a unique IP address subnet. You cable the LAN NIC from the back of the FBSD gateway PC to a network switch or hub. A small cheap switch normally has 5 plugs. One plug for each PC on the LAN including the FBSD gateway. You run a cable from the switch to the NIC of each PC you want on the LAN. A LAN circuit can handle many PC's and many downstream switches as long as the max distance of the cable is not exceeded. To add more LAN users you create another circuit by adding another NIC in the FBSD box connected to another switch which has more LAN PC's connected to it. Please note, this is a very simplified LAN description and layout, but is adequate for basic understanding of how the physical parts of the LAN are cabled together.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For the home user with just two PCs, you can cable your FBSD LAN NIC directly to the other PC's NIC with a special crossover cable.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Installing the LAN ==&lt;br /&gt;
&lt;br /&gt;
Your PC should have two NIC already installed. &lt;br /&gt;
During the boot of FBSD review the /var/run/dmesg.boot log to verify that your PCI NIC were found. &lt;br /&gt;
&lt;br /&gt;
This is what you are looking for. This means that FBSD GENERIC kernel found your NIC. The dc0 name will be different depending on the manufacture of your NIC.&lt;br /&gt;
&lt;br /&gt;
dc0: &amp;lt;Macronix 98715/98715A 10/100BaseTX&amp;gt; port 0xdc00-0xdcff mem &lt;br /&gt;
0xe3000000-0xe30000ff irq 3 at device 19.0 on pci0&lt;br /&gt;
dc0: Ethernet address: 00:80:c6:f2:2e:3b&lt;br /&gt;
miibus0: &amp;lt;MII bus&amp;gt; on dc0&amp;gt;&lt;br /&gt;
dcphy0: &amp;lt;Intel 21143 NWAY media interface&amp;gt; on miibus0&lt;br /&gt;
dcphy0: 10baseT, 10baseT-FDX, 100baseTX, 100baseTX-FDX, auto&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dc0 is the NIC FBSD internal interface name.&lt;br /&gt;
&lt;br /&gt;
The generic kernel contains device statements for most of the NIC currently on the market. If the /var/run/dmesg.boot log shows your NIC as&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
pci0: &amp;lt;unknown card&amp;gt; (vendor=0x1274, dev=0x5000) at 19.0&lt;br /&gt;
&lt;br /&gt;
or no message to indicate the probe of the BOIS found any PCI devices, then you may have an older BOIS on your PC which does not handle PCI cards very well. On older (IE: pre Y2K) PC BOIS, it's very common for the system probe process of the bio's to be unable to find one or more PCI cards. If this happens to you, you have to do some research to determine the problem.&lt;br /&gt;
&lt;br /&gt;
Try the pciconf -lv command to see if it gives you any useful info. Then review the GENERIC source at /usr/src/sys/i386/conf/GENERIC to see if it contains any device statement comments about your NIC based on the manufacturer or chips used. If you do find a device statement in the GENERIC source for your NIC, then add this statement to your kernel source and recompile your kernel.&lt;br /&gt;
&lt;br /&gt;
device pun&lt;br /&gt;
&lt;br /&gt;
This device has additional code to probe your system's BIOS using different methods which in most cases results in your PCI NIC being found.&lt;br /&gt;
&lt;br /&gt;
If the review of the GENERIC kernel source produces no results, them review the kernel source file named LINT at /usr/src/sys/i386/conf/ for comments that describe your NIC by manufacturer name or chips used. Copy the appropriate device statements to the GENERIC kernel source file and then follow the instructions at Kernel Customizing. You will have to create a custom kernel from the GENERIC source including the device statement from the LINT source.&lt;br /&gt;
&lt;br /&gt;
If you find no kernel device statements for your NIC, then it's not supported and you have to get one that is.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== LAN private IP address ==&lt;br /&gt;
&lt;br /&gt;
There are ranges of special IP addresses reserved for use on private LANs. These special IP address ranges are non-routable on the public Internet. They are listed in the /etc/hosts file.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP address ranges for&lt;br /&gt;
private networks which will never be connected to the Internet:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
10.0.0.0    - 10.255.255.255 &lt;br /&gt;
172.16.0.0  - 172.31.255.255&lt;br /&gt;
192.168.0.0 - 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
These can also be written as&lt;br /&gt;
10.0.0.0/8 &lt;br /&gt;
172.16.0.0/12 &lt;br /&gt;
192.168.0.0/16&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
To communicate with the LAN PC's the FBSD system needs to know what the IP address range of the PC's on the LAN is and the LAN PC's needs to be configured with LAN network information so they know how to perform their part in the communication process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are two ways to accomplish this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The manually way by hand, or&lt;br /&gt;
&lt;br /&gt;
The automatic way using the FBSD port application DHCP.&lt;br /&gt;
&lt;br /&gt;
== Manually Configuring the Gateway host ==&lt;br /&gt;
 &lt;br /&gt;
Before you can manually configure each PC on the LAN by hand, you first have to collect some information from your FBSD gateway box. It's assumed you already have your gateway PC connection to the public Internet already working. &lt;br /&gt;
&lt;br /&gt;
The configuration file /etc/resolv.conf is automatically populated with the IP address of your ISP's primary and secondary domain name servers every time you log in to your ISP. Write down these IP addresses you will need them to configure your LAN PCs.&lt;br /&gt;
&lt;br /&gt;
Now you decide on the private IP address range to use for your LAN. This guide uses a very small portion of the 10.0.0.0/8 range for the private Lan, which is 10.0.10.0/29. This gives 10.0.10.0 through 10.0.10.7. The usable portion of the range is 10.0.10.1 through 10.0.10.6, the 10.0.10.0 and 10.0.10.7 is the reserved pair for broadcasting. &lt;br /&gt;
&lt;br /&gt;
The IP address of the NIC in the FBSD gateway will be 10.0.10.2. The IP address of the first LAN PC to be manually configured will be 10.0.10.3.&lt;br /&gt;
&lt;br /&gt;
On the FBSD gateway system add these two statements to /etc/rc.conf to manually assign the FBSD LAN NIC an IP address and tell FBSD to act as a gateway for the LAN.&lt;br /&gt;
&lt;br /&gt;
ifconfig_dc0=&amp;quot;inet 10.0.10.2 netmask 255.255.255.248&amp;quot;&lt;br /&gt;
&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dc0 is the gateway interface name of the NIC the LAN is cabled to.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually configuring LAN FBSD PC ==&lt;br /&gt;
&lt;br /&gt;
For a FBSD workstation PC on the LAN, add these statements to /etc/rc.conf to manually assign the FBSD LAN NIC an IP address. Be sure to change dc0 to the interface name of the Nic card in the FBSD LAN PC.&lt;br /&gt;
&lt;br /&gt;
ifconfig_dc0=&amp;quot;inet 10.0.10.3 netmask 255.255.255.248&amp;quot;&lt;br /&gt;
&lt;br /&gt;
defaultrouter=&amp;quot;10.0.10.2&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Copy the FBSD gateway /etc/resolv.conf file to the FBSD LAN PC, replacing the one that's there. Or edit the FBSD LAN PC's /etc/resolv.conf so it's the same as the one from the FBSD gateway. Reboot system to enable your changes.&lt;br /&gt;
&lt;br /&gt;
To test, ping the gateway server:&lt;br /&gt;
&lt;br /&gt;
ping -c 4 10.0.10.2&lt;br /&gt;
&lt;br /&gt;
Then test DNS by pinging:&lt;br /&gt;
&lt;br /&gt;
ping -c 4 freebsd.org&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually config MS/Windows LAN PC ==&lt;br /&gt;
 &lt;br /&gt;
This procedure has been tested on MS/Windows 98, ME and XP.&lt;br /&gt;
&lt;br /&gt;
Click on start, settings, control panel, networking. In the window the installed network components are displayed. Scroll through them and click to highlight the TCP/IP line for the NIC you are going to use to connect this box to your LAN. When it's highlighted, the properties button below the window becomes enabled. Click on the properties button and a window pops up which is where you manually configure the NIC TCP/IP network settings.&lt;br /&gt;
&lt;br /&gt;
Under the IP address tab, click on specify IP address. For IP address enter 10.0.10.4&lt;br /&gt;
&lt;br /&gt;
Under the gateway tab, new gateway window enter the IP address of the FBSD gateway&lt;br /&gt;
&lt;br /&gt;
10.0.10.2 and click the add button.&lt;br /&gt;
&lt;br /&gt;
Under the DNS configuration tab, click on enable DNS. In the DNS server search order window enter the first of the two IP addresses you got from the FBSD gateway /etc/resolv.conf file. Click on the add button, then do same thing over again for the second IP address. When you're finished click on the OK button at the bottom of the pop up window, and click OK again. The system will reboot to activate your changes.&lt;br /&gt;
&lt;br /&gt;
To test, click on start, run.&lt;br /&gt;
&lt;br /&gt;
Enter C:\windows\command.com&lt;br /&gt;
&lt;br /&gt;
When a native DOS window opens, ping the gateway server: &lt;br /&gt;
&lt;br /&gt;
Ping 10.0.10.2&lt;br /&gt;
&lt;br /&gt;
Then test DNS:&lt;br /&gt;
&lt;br /&gt;
ping freebsd.org&lt;br /&gt;
&lt;br /&gt;
The DNS servers will convert this domain name to an IP address and then send four pings to it. When this has completed, enter exit to leave native DOS mode.&lt;br /&gt;
&lt;br /&gt;
For each additional FBSD or MS/Windows LAN PCs you want to add, just increment the last digit of the last assigned IP address by 1. You may have to keep a log book so you know what LAN IP addresses you have assigned. All LAN PCs connected to the FBSD gateway 10.0.10.2 NIC have to use the same IP address sub-net, (IE: 10.0.10.x) where in this example x can be 1 through 6.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP (Dynamic Host Configuration Protocol) ==&lt;br /&gt;
 &lt;br /&gt;
If you are following the 'incremental install method' recommended in this Installers Guide, you have now completed the basic install of the FBSD Gateway/Firewall server with attached LAN. Everything up to this point has been accomplished using the built in facilities available in the standard FBSD stable release. &lt;br /&gt;
&lt;br /&gt;
In the previous section you manually configured your LAN PC's by hand with the information they needed to communicate with the FBSD gateway. DHCP is used to automate and control the automatic assignment of private IP addresses to your LAN environment. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
What function does DHCP perform?&lt;br /&gt;
The Dynamic Host Configuration Protocol (DHCP) is most commonly used in the situation where a LAN (local area network) has too many PC workstations for the LAN administrator to manually configuration each workstation with the information it needs to use for access on the LAN. To automate this process, DHCP was developed. DHCP usually runs on the gateway/firewall machine in server mode. It broadcasts its presence through the LAN to all the workstations who have a DHCP client version of DHCP installed. At workstation boot up it asks the DHCP server for the information necessary to configure itself for access to LAN services. &lt;br /&gt;
&lt;br /&gt;
All Microsoft Windows machines have a DHCP client built in that defaults to using DHCP services without any user configuration. FBSD also has a built in DHCP client, but it needs manual user input to activate it. Many ISP's use DHCP on dial up, DSL, and cable access to achieve the same results a LAN administrator wants for his private LAN.&lt;br /&gt;
&lt;br /&gt;
One of DHCP's major strengths is its ability to manage the dynamic assignment of IP addresses from a pool and to reuse any IP address released when a workstation is removed from the LAN or moved to a different location on the LAN, such as what normally happens in a company work place environment. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP Server ==&lt;br /&gt;
 &lt;br /&gt;
To add a DHCP server to FBSD you have to install the port. The best and most commonly used port for this purpose is the isc-dhcpd3 port. &lt;br /&gt;
The ISC-DHCP3 server supports three mechanisms for IP address allocation. In &amp;quot;automatic allocation&amp;quot;, DHCP assigns a permanent IP address to a client. In &amp;quot;dynamic allocation&amp;quot;, DHCP assigns an IP address to a client for a limited period of time (or until the client explicitly relinquishes the address). In &amp;quot;manual allocation&amp;quot;, a client's IP address is assigned by the network administrator, and DHCP is used simply to convey the assigned address to the client. Dynamic allocation is the only one of the three mechanisms that allows automatic reuse of am address that is no longer needed by the client to which it was assigned. A particular network will use one or more of these mechanisms, depending on the policies of the network administrator.&lt;br /&gt;
&lt;br /&gt;
For our purpose of a simple DHCP server that would fill the needs of the common FBSD user we are going to configure the DHCP server for &amp;quot;dynamic allocation&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How DHCP Works ==&lt;br /&gt;
&lt;br /&gt;
When the dhcpd daemon starts up at FBSD boot time, it broadcasts its presence through the LAN, then it sleeps and listens for broadcast requests for network configuration information from the LAN workstations. By default, it will listen on UDP port 67. When such a request is received, then the server will reply to the client machine on UDP port 68, providing the details required to connect to the network such as the IP address assigned to the workstation, subnet mask, default gateway and DNS servers names or IP addresses. Also included with this reply is a length of time for which this information can be used by that particular client. This is known as a DHCP &amp;quot;lease&amp;quot; and a new lease must be acquired by the client when it expires. The length of time for which a lease is valid is decided by the administrator of the DHCP server. The DHCP server keeps a database of leases it has issued in /var/db/dhcpd.leases File. This file is written as a log and can be edited. See man dhcpd.leases which gives a slightly longer description. DHCP clients can obtain a great deal of information from the server. An exhaustive list may be found in man dhcp-options &amp;amp; man dhcpd after DHCP is installed.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP Configuration Instructions ==&lt;br /&gt;
&lt;br /&gt;
To install the DHCP software, use the FBSD dhcp package using the following command &lt;br /&gt;
&lt;br /&gt;
pkg_add -rv isc-dhcp3-server&lt;br /&gt;
&lt;br /&gt;
To start the DHCPD server at boot time add the following statements in the /etc/rc.conf file.&lt;br /&gt;
&lt;br /&gt;
ee /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
dhcpd_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
dhcpd_conf=&amp;quot;/usr/local/etc/dhcpd.conf&amp;quot;&lt;br /&gt;
dhcpd_ifaces=&amp;quot;xl0&amp;quot;&lt;br /&gt;
dhcpd_flags=&amp;quot;-q&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The -q option will turn off the copyright banner that displays during the FBSD boot up and in the DHCP log every time a broadcast is issued by the DHCP daemon or when a request is received from a workstation DHCP client.&lt;br /&gt;
&lt;br /&gt;
The dc0 is to be replaced with the interface name of the LAN NIC you want DHCP service on from your gateway/firewall FBSD system. &lt;br /&gt;
&lt;br /&gt;
The dhcpd.conf file is delivered as a sample file so you have to make a copy of it without its sample suffix. It contains a lot of comments and commented out statement examples which you can comment out or delete. Edit the main DHCP configuration file and make it look like this.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
cp dhcpd.conf.sample dhcpd.conf&lt;br /&gt;
&lt;br /&gt;
ee dhcpd.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
option domain-name &amp;quot;fbsdjones.com&amp;quot;;&lt;br /&gt;
option domain-name-servers 208.206.15.11, 208.206.15.12;&lt;br /&gt;
# 600=10min, 7200=2 hours, 86400=1 day, 604800=1 week, 2592000=30 days&lt;br /&gt;
default-lease-time 86400;&lt;br /&gt;
max-lease-time 604800;&lt;br /&gt;
authoritative;&lt;br /&gt;
ddns-update-style none;&lt;br /&gt;
log-facility local1;&lt;br /&gt;
# No service will be given on this subnet, but declaring it helps the &lt;br /&gt;
# DHCP server to understand the network topology.&lt;br /&gt;
subnet 10.152.187.0 netmask 255.255.255.0 { }&lt;br /&gt;
&lt;br /&gt;
# This is the fbsdjones.com subnet declaration.&lt;br /&gt;
# Max of 6 pc on LAN 10.0.10.1 - 10.0.10.6&lt;br /&gt;
# 10.0.10.2 is the IP address of the Nic card in FBSD&lt;br /&gt;
# 10.0.10.7 is the broadcast IP address &lt;br /&gt;
subnet 10.0.10.0 netmask 255.255.255.248 {&lt;br /&gt;
range 10.0.10.1 10.0.10.6;&lt;br /&gt;
option routers 10.0.10.2;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option domain-name &amp;quot;fbsdjones.com&amp;quot;; is the user selected domain name from the hostname=&amp;quot;gateway.fbsdjones.com&amp;quot; statement of /etc/rc.conf. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option domain-name-servers contains the DSN server's IP addresses of your ISP from /etc/resolv.conf nameserver statements which get populated automatically when you connect to your ISP. If you have your own private LAN domain DSN server, make it the first one in the list, and in that case you can use full domain names instead of IP address (such as dnslocal.fbsdjones.com, dsn1.isp-domain.com).&lt;br /&gt;
&lt;br /&gt;
The default-lease-time and max-lease-time have values in seconds to set the elapse period for these function. The values I show are good to go with.&lt;br /&gt;
&lt;br /&gt;
The authoritative; options tells the DHCP daemon server that it is the boss and is in control of issuing all the information to the LAN DHCP clients.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The ddns-update-style none; tells DHCP that there is no local LAN DSN server. If you have one, change this from none to interim. In the dhcpd.conf.sample you will see comments saying none and ad-hoc are the two options. This is no longer true for DHCP version 3.0. Ad-hoc has been deactivated and replaced with interim. See man dhcpd.conf for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The log-facility allows you to segregate the DHCP messages to a separate log for recording. You are going to use local1 for logging of DHCP server error messages;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
subnet 10.0.10.0 netmask 255.255.255.248 {&lt;br /&gt;
range 10.0.10.1 10.0.10.6;&lt;br /&gt;
option routers 10.0.10.2; }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The subnet 10.0.10.0 netmask 255.255.255.248 statement declares the maximum subnet IP address range. In this case the last three digits in the netmask, 248 determines the range. This means a total of 8 IP addresses, 10.0.10.0 through 10.0.10.7 are allocated as the subnet range. 10.0.10.0 and 10.0.10.7 are reserved for the broadcast process. &lt;br /&gt;
&lt;br /&gt;
The range 10.0.10.1 10.0.10.6; is saying this range of IP addresses makes up the pool of addresses that are to be used for dynamic IP allocation to DHCP clients. It's a small home LAN with only two MS/Windows boxes and a single FBSD box on it now. That can grow to six machines without making any changes to this statement group. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option routers 10.0.10.2 statement is a bit miss-leading. What this is referring to is the NIC in the FBSD box the DHCP server runs on and the LAN being configured is cabled to. In our case the NIC has an IP address of 10.0.10.2 which is specified in /etc/rc.conf by the ifconfig_dc0=&amp;quot;inet 10.0.10.2 netmask 255.255.255.248&amp;quot; statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The principle behind bitmasks and netmasks is simple, but often confusing to new users as it requires knowledge of binary numbers. For a quick reference, the following table illustrates what network ranges are indicated by the corresponding bitmasks/netmasks up to a default class C netmask. &lt;br /&gt;
&lt;br /&gt;
Bitmask   Netmask          Total IP's /  Usable IP's&lt;br /&gt;
  32      255.255.255.255         1              1&lt;br /&gt;
  31      255.255.255.254         2              1&lt;br /&gt;
  30      255.255.255.252         4              2&lt;br /&gt;
  29      255.255.255.248         8              6&lt;br /&gt;
  28      255.255.255.240        16             14&lt;br /&gt;
  27      255.255.255.224        32             30&lt;br /&gt;
  26      255.255.255.192        64             62&lt;br /&gt;
  25      255.255.255.128       128            126&lt;br /&gt;
  24      255.255.255.0         256            254&lt;br /&gt;
  22      255.255.192.0       16320          16318&lt;br /&gt;
  20      255.255.128.0       32768          32766&lt;br /&gt;
  16      255.255.0.0         65536          65534&lt;br /&gt;
  12      255.128.0.0   8.388608+e6    8.388606+e6 &lt;br /&gt;
   8      255.0.0.0           256^3      (256^3)-2&lt;br /&gt;
   0      0.0.0.0  (all IP's) 256^4       (256^4)-2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, there is a definite pattern. The number of total IP's always doubles, and the number of usable IP's is always total - 2. This is because for every IP network/subnet there are two IP's reserved for the network and broadcast addresses. The netmask's last octet starts at 255 and constantly decreases by multiples of 2, while the bitmask decreases by multiples of 1, because in binary, each shift over to the left halves the number, not divides by ten like in the decimal number system. This same pattern goes for all possible netmasks and bitmasks. &lt;br /&gt;
&lt;br /&gt;
Since you told DHCPD to use local1 for logging in the dhcpd.conf configuration file above, you now have to complete the logging environment configuration by adding the following statement to /etc/syslog.conf. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
ee /etc/syslog.conf &lt;br /&gt;
&lt;br /&gt;
local1.notice         /var/log/dhcpd.log&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
This log file does not exist, so you must create it.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/dhcpd.log&lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Now you must set up log rotation. Add this statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ee /etc/newsyslog.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/var/log/dhcp.log          600 3 100 * B&lt;br /&gt;
&lt;br /&gt;
You can change the log rotation triggers to whatever you want.&lt;br /&gt;
See man newsyslog for info on what the trigger values mean.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The DHCPD daemon has a start up script located at /usr/local/etc/rc.d/&lt;br /&gt;
&lt;br /&gt;
This directory location is where FBSD looks for files that end in .sh and executes them at the end of the boot process to start the applications. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can administer the DHCPD server from the command line using&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/isc-dhcp.sh start &lt;br /&gt;
                                stop&lt;br /&gt;
                                restart&lt;br /&gt;
                                &lt;br /&gt;
Restart is used to reread dhcp.conf file after making changes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now manually start DHCP by entering this on the command.&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/isc-dhcp.sh start &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Issue 'ps ax' command to see the DHCP daemon running in the active task list. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Testing the DHCPD Daemon &lt;br /&gt;
To test the DHCPD server you need a PC on the LAN. &lt;br /&gt;
&lt;br /&gt;
First let's check the LAN MS/Windows box network configuration. Click on the following buttons in this order. Start/settings/control panel/network/. Highlight TCP/IP and click on properties button. In the IP address tab the 'obtain IP address automatically' should be to only thing check marked. All the fields in the other tabs must be blank. If this is what you have use the cancel buttons to back yourself out. If you answer ok, you may have to have the windows install CDROM to update the network section.&lt;br /&gt;
&lt;br /&gt;
Windows 98, 2000, and ME have a program c:/windows/winipcfg.exe which will show you the DHCP info it's using. Start the winipcfg program by clicking on start, run, and type c:/windows/winipcfg.exe into the run window and then hit the OK button. Click on the more info button to see everything. You should be able to comprehend what you see back to the dhcpd.conf options as explained above. Click on the 'renew all' button to acquire a new DHCP lease. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FBSD as a DHCP Client ==&lt;br /&gt;
 &lt;br /&gt;
The isc-dhcp3 port comes with a client. I am not going to cover the isc-dhcp3 port client configuration process, because FBSD comes with a DHCP client built into the basic FBSD system. &lt;br /&gt;
&lt;br /&gt;
To activate the built in dhcp client on a FBSD LAN PC, edit /etc/rc.conf and add the following statement to tell FBSD what interface the client DHCP should use:&lt;br /&gt;
&lt;br /&gt;
ee /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ifconfig_dc0=&amp;quot;DHCP&amp;quot;     # Where dc0 is the FBSD Nic card interface name.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That's it, configuration complete. Reboot to activate your changes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Local_Area_Network_(LAN)</id>
		<title>Local Area Network (LAN)</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Local_Area_Network_(LAN)"/>
				<updated>2012-08-13T23:54:31Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Setting up Local Area Network (LAN) ==&lt;br /&gt;
&lt;br /&gt;
A local area network (LAN) is a group of computers and associated devices that share a common communications line or wireless link and typically share the resources of a single processor or server within a small geographic area (for example, within an office building). Usually, the server has applications and data storage that are shared in common by multiple LAN computer users. A local area network may serve as few as two or three users (for example, in a home network) or as many as thousands of users (for example, in an FDDI network). Typically, a suite of application programs can be kept on the LAN server. Users who need an application frequently can download it once and then run it from their local hard disk. A user can share files with others at the LAN server. &lt;br /&gt;
&lt;br /&gt;
There are many technical limitations and options to how a LAN is configured depending on if you are a non-commercial user or commercial user and how many of the MS/Windows and/or Unix network server sharing facilities you want to enable. &lt;br /&gt;
&lt;br /&gt;
Basically what determines if you are commercial user or not is how you are known to the public Internet. A commercial user has a permanent, dedicated, high-speed leased Internet line connecting them to their ISP and one or more static IP addresses assigned by their ISP. A static IP address is always the same number; it never changes between logins to the ISP. They have an official registered domain name that points to one of the static IP address which points to their PC that is acting as their gateway. If the commercial user pays for a large block of static IP addresses then they can use these IP address for the computers on the LAN and not need to use NAT (network address translation). Their email will arrive at the gateway PC and is processed by their mail server directly. They do not use their ISP to receive and hold their email for them.&lt;br /&gt;
&lt;br /&gt;
An non-commercial user, like the home user, uses a phone line dial in login to their ISP on a limited speed connection or has a 24/7 cable or DSL high speed connection and gets assigned a single dynamic IP address which changes every time they login. Their ISP receives and holds all their email for them. The only way a public Internet user can find them is if that know the dynamic IP address currently in use by them. From the ISP viewpoint a non-commercial user uses a very small amount of its overall resources and so charges much less for a single user account.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FBSD system that is acting as the gateway can also be configured to provide different levels of network sharing depending on what kind of operating systems are running on the PCs connected to the LAN. For Unix-like operating systems NFS provides network file and device sharing, while the FBSD port application Samba does the same thing for MS/Windows PCs on the LAN. These facilities, NFS and Samba are not covered in this guide as they are more applicable to commercial users who have large LANs.&lt;br /&gt;
&lt;br /&gt;
See this link for Samba details http://us1.samba.org/samba/samba.html&lt;br /&gt;
&lt;br /&gt;
See the following links for details on FBSD NFS:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/network-nfs.html&lt;br /&gt;
&lt;br /&gt;
http://www.onlamp.com/pub/a/bsd/2000/07/26/FreeBSD_Basics.html&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Home User LANs ==&lt;br /&gt;
&lt;br /&gt;
Normally each family member would have to have their own phone line and unique ISP account to connect all of the family PCs to the Internet simultaneously. This is a costly way of doing this. The alternative is to have a single FBSD system gateway connect to an ISP and then network the other family members PC's behind the gateway using private IP addresses and NAT (Network Addressing Translation) so everything leaving the gateway system looks like it came from the single dynamic IP address assigned by the ISP. Your ISP can not tell if the packet passing through them has been NATed or not.&lt;br /&gt;
&lt;br /&gt;
Installer Note: When you sign up for service with your ISP you have to sign a user agreement that basically says you are not allowed to do NAT on your PC or run email services or web servers. If you are caught, it's grounds for them to terminate your account. Never tell your ISP tech support people what you are doing. Most ISP's leave open all the ports except the port used by an email server, which they block. More recently some ISPs have started to also block the web server port number.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another simple to configure facility is an anonymous FTP server on the FBSD gateway so LAN users can post files there that they want to pass to other LAN users. This allows them to pass large files between LAN users.&lt;br /&gt;
&lt;br /&gt;
The LAN can be populated with both MS/Windows boxes and FBSD boxes and not cause any problems. ISP's usually allow 5 email address per dial in account. Each family member can have their own email address and using the email client on their PC get their email directly from the ISP email server. Or you can run a task on the FBSD gateway box to download the email from the ISP account on a recurring schedule and store it in the FBSD built in email server called sendmail, and then have all the LAN users get their email from the sendmail server without having the FBSD gateway connected to the Internet.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Topography of a LAN&lt;br /&gt;
        __________         ________             _____ &lt;br /&gt;
       |          |       |        |           |     | &lt;br /&gt;
       |  FBSD/GW |       | switch |&amp;lt;---------&amp;gt;| LAN |&lt;br /&gt;
       |          |       | or     |           | PC1 |    _____&lt;br /&gt;
       |      NIC |&amp;lt;-----&amp;gt;| hub    |&amp;lt;--|       |_____|   |     |&lt;br /&gt;
       |__________|       |________|   |                 | LAN |&lt;br /&gt;
                                       |&amp;lt;---------------&amp;gt;| PC2 |&lt;br /&gt;
                                                         |_____| &lt;br /&gt;
                                                             &lt;br /&gt;
                                                            &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The above diagram shows a simple single LAN circuit. Your FBSD gateway box needs a NIC for each separate LAN circuit. Each circuit must use a unique IP address subnet. You cable the LAN NIC from the back of the FBSD gateway PC to a network switch or hub. A small cheap switch normally has 5 plugs. One plug for each PC on the LAN including the FBSD gateway. You run a cable from the switch to the NIC of each PC you want on the LAN. A LAN circuit can handle many PC's and many downstream switches as long as the max distance of the cable is not exceeded. To add more LAN users you create another circuit by adding another NIC in the FBSD box connected to another switch which has more LAN PC's connected to it. Please note, this is a very simplified LAN description and layout, but is adequate for basic understanding of how the physical parts of the LAN are cabled together.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For the home user with just two PCs, you can cable your FBSD LAN NIC directly to the other PC's NIC with a special crossover cable.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Installing the LAN ==&lt;br /&gt;
&lt;br /&gt;
Your PC should have two NIC already installed. &lt;br /&gt;
During the boot of FBSD review the /var/run/dmesg.boot log to verify that your PCI NIC were found. &lt;br /&gt;
&lt;br /&gt;
This is what you are looking for. This means that FBSD GENERIC kernel found your NIC. The dc0 name will be different depending on the manufacture of your NIC.&lt;br /&gt;
&lt;br /&gt;
dc0: &amp;lt;Macronix 98715/98715A 10/100BaseTX&amp;gt; port 0xdc00-0xdcff mem &lt;br /&gt;
0xe3000000-0xe30000ff irq 3 at device 19.0 on pci0&lt;br /&gt;
dc0: Ethernet address: 00:80:c6:f2:2e:3b&lt;br /&gt;
miibus0: &amp;lt;MII bus&amp;gt; on dc0&amp;gt;&lt;br /&gt;
dcphy0: &amp;lt;Intel 21143 NWAY media interface&amp;gt; on miibus0&lt;br /&gt;
dcphy0: 10baseT, 10baseT-FDX, 100baseTX, 100baseTX-FDX, auto&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dc0 is the NIC FBSD internal interface name.&lt;br /&gt;
&lt;br /&gt;
The generic kernel contains device statements for most of the NIC currently on the market. If the /var/run/dmesg.boot log shows your NIC as&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
pci0: &amp;lt;unknown card&amp;gt; (vendor=0x1274, dev=0x5000) at 19.0&lt;br /&gt;
&lt;br /&gt;
or no message to indicate the probe of the BOIS found any PCI devices, then you may have an older BOIS on your PC which does not handle PCI cards very well. On older (IE: pre Y2K) PC BOIS, it's very common for the system probe process of the bio's to be unable to find one or more PCI cards. If this happens to you, you have to do some research to determine the problem.&lt;br /&gt;
&lt;br /&gt;
Try the pciconf -lv command to see if it gives you any useful info. Then review the GENERIC source at /usr/src/sys/i386/conf/GENERIC to see if it contains any device statement comments about your NIC based on the manufacturer or chips used. If you do find a device statement in the GENERIC source for your NIC, then add this statement to your kernel source and recompile your kernel.&lt;br /&gt;
&lt;br /&gt;
device pun&lt;br /&gt;
&lt;br /&gt;
This device has additional code to probe your system's BIOS using different methods which in most cases results in your PCI NIC being found.&lt;br /&gt;
&lt;br /&gt;
If the review of the GENERIC kernel source produces no results, them review the kernel source file named LINT at /usr/src/sys/i386/conf/ for comments that describe your NIC by manufacturer name or chips used. Copy the appropriate device statements to the GENERIC kernel source file and then follow the instructions at Kernel Customizing. You will have to create a custom kernel from the GENERIC source including the device statement from the LINT source.&lt;br /&gt;
&lt;br /&gt;
If you find no kernel device statements for your NIC, then it's not supported and you have to get one that is.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== LAN private IP address ==&lt;br /&gt;
&lt;br /&gt;
There are ranges of special IP addresses reserved for use on private LANs. These special IP address ranges are non-routable on the public Internet. They are listed in the /etc/hosts file.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP address ranges for&lt;br /&gt;
private networks which will never be connected to the Internet:&lt;br /&gt;
&lt;br /&gt;
10.0.0.0    - 10.255.255.255 &lt;br /&gt;
172.16.0.0  - 172.31.255.255&lt;br /&gt;
192.168.0.0 - 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
These can also be written as&lt;br /&gt;
10.0.0.0/8 &lt;br /&gt;
172.16.0.0/12 &lt;br /&gt;
192.168.0.0/16&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
To communicate with the LAN PC's the FBSD system needs to know what the IP address range of the PC's on the LAN is and the LAN PC's needs to be configured with LAN network information so they know how to perform their part in the communication process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are two ways to accomplish this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The manually way by hand, or&lt;br /&gt;
&lt;br /&gt;
The automatic way using the FBSD port application DHCP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually Configuring the Gateway host ==&lt;br /&gt;
 &lt;br /&gt;
Before you can manually configure each PC on the LAN by hand, you first have to collect some information from your FBSD gateway box. It's assumed you already have your gateway PC connection to the public Internet already working. &lt;br /&gt;
&lt;br /&gt;
The configuration file /etc/resolv.conf is automatically populated with the IP address of your ISP's primary and secondary domain name servers every time you log in to your ISP. Write down these IP addresses you will need them to configure your LAN PCs.&lt;br /&gt;
&lt;br /&gt;
Now you decide on the private IP address range to use for your LAN. This guide uses a very small portion of the 10.0.0.0/8 range for the private Lan, which is 10.0.10.0/29. This gives 10.0.10.0 through 10.0.10.7. The usable portion of the range is 10.0.10.1 through 10.0.10.6, the 10.0.10.0 and 10.0.10.7 is the reserved pair for broadcasting. &lt;br /&gt;
&lt;br /&gt;
The IP address of the NIC in the FBSD gateway will be 10.0.10.2. The IP address of the first LAN PC to be manually configured will be 10.0.10.3.&lt;br /&gt;
&lt;br /&gt;
On the FBSD gateway system add these two statements to /etc/rc.conf to manually assign the FBSD LAN NIC an IP address and tell FBSD to act as a gateway for the LAN.&lt;br /&gt;
&lt;br /&gt;
ifconfig_dc0=&amp;quot;inet 10.0.10.2 netmask 255.255.255.248&amp;quot;&lt;br /&gt;
&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dc0 is the gateway interface name of the NIC the LAN is cabled to.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually configuring LAN FBSD PC ==&lt;br /&gt;
&lt;br /&gt;
For a FBSD workstation PC on the LAN, add these statements to /etc/rc.conf to manually assign the FBSD LAN NIC an IP address. Be sure to change dc0 to the interface name of the Nic card in the FBSD LAN PC.&lt;br /&gt;
&lt;br /&gt;
ifconfig_dc0=&amp;quot;inet 10.0.10.3 netmask 255.255.255.248&amp;quot;&lt;br /&gt;
&lt;br /&gt;
defaultrouter=&amp;quot;10.0.10.2&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Copy the FBSD gateway /etc/resolv.conf file to the FBSD LAN PC, replacing the one that's there. Or edit the FBSD LAN PC's /etc/resolv.conf so it's the same as the one from the FBSD gateway. Reboot system to enable your changes.&lt;br /&gt;
&lt;br /&gt;
To test, ping the gateway server:&lt;br /&gt;
&lt;br /&gt;
ping -c 4 10.0.10.2&lt;br /&gt;
&lt;br /&gt;
Then test DNS by pinging:&lt;br /&gt;
&lt;br /&gt;
ping -c 4 freebsd.org&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually config MS/Windows LAN PC ==&lt;br /&gt;
 &lt;br /&gt;
This procedure has been tested on MS/Windows 98, ME and XP.&lt;br /&gt;
&lt;br /&gt;
Click on start, settings, control panel, networking. In the window the installed network components are displayed. Scroll through them and click to highlight the TCP/IP line for the NIC you are going to use to connect this box to your LAN. When it's highlighted, the properties button below the window becomes enabled. Click on the properties button and a window pops up which is where you manually configure the NIC TCP/IP network settings.&lt;br /&gt;
&lt;br /&gt;
Under the IP address tab, click on specify IP address. For IP address enter 10.0.10.4&lt;br /&gt;
&lt;br /&gt;
Under the gateway tab, new gateway window enter the IP address of the FBSD gateway&lt;br /&gt;
&lt;br /&gt;
10.0.10.2 and click the add button.&lt;br /&gt;
&lt;br /&gt;
Under the DNS configuration tab, click on enable DNS. In the DNS server search order window enter the first of the two IP addresses you got from the FBSD gateway /etc/resolv.conf file. Click on the add button, then do same thing over again for the second IP address. When you're finished click on the OK button at the bottom of the pop up window, and click OK again. The system will reboot to activate your changes.&lt;br /&gt;
&lt;br /&gt;
To test, click on start, run.&lt;br /&gt;
&lt;br /&gt;
Enter C:\windows\command.com&lt;br /&gt;
&lt;br /&gt;
When a native DOS window opens, ping the gateway server: &lt;br /&gt;
&lt;br /&gt;
Ping 10.0.10.2&lt;br /&gt;
&lt;br /&gt;
Then test DNS:&lt;br /&gt;
&lt;br /&gt;
ping freebsd.org&lt;br /&gt;
&lt;br /&gt;
The DNS servers will convert this domain name to an IP address and then send four pings to it. When this has completed, enter exit to leave native DOS mode.&lt;br /&gt;
&lt;br /&gt;
For each additional FBSD or MS/Windows LAN PCs you want to add, just increment the last digit of the last assigned IP address by 1. You may have to keep a log book so you know what LAN IP addresses you have assigned. All LAN PCs connected to the FBSD gateway 10.0.10.2 NIC have to use the same IP address sub-net, (IE: 10.0.10.x) where in this example x can be 1 through 6.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP (Dynamic Host Configuration Protocol) ==&lt;br /&gt;
 &lt;br /&gt;
If you are following the 'incremental install method' recommended in this Installers Guide, you have now completed the basic install of the FBSD Gateway/Firewall server with attached LAN. Everything up to this point has been accomplished using the built in facilities available in the standard FBSD stable release. &lt;br /&gt;
&lt;br /&gt;
In the previous section you manually configured your LAN PC's by hand with the information they needed to communicate with the FBSD gateway. DHCP is used to automate and control the automatic assignment of private IP addresses to your LAN environment. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
What function does DHCP perform?&lt;br /&gt;
The Dynamic Host Configuration Protocol (DHCP) is most commonly used in the situation where a LAN (local area network) has too many PC workstations for the LAN administrator to manually configuration each workstation with the information it needs to use for access on the LAN. To automate this process, DHCP was developed. DHCP usually runs on the gateway/firewall machine in server mode. It broadcasts its presence through the LAN to all the workstations who have a DHCP client version of DHCP installed. At workstation boot up it asks the DHCP server for the information necessary to configure itself for access to LAN services. &lt;br /&gt;
&lt;br /&gt;
All Microsoft Windows machines have a DHCP client built in that defaults to using DHCP services without any user configuration. FBSD also has a built in DHCP client, but it needs manual user input to activate it. Many ISP's use DHCP on dial up, DSL, and cable access to achieve the same results a LAN administrator wants for his private LAN.&lt;br /&gt;
&lt;br /&gt;
One of DHCP's major strengths is its ability to manage the dynamic assignment of IP addresses from a pool and to reuse any IP address released when a workstation is removed from the LAN or moved to a different location on the LAN, such as what normally happens in a company work place environment. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP Server ==&lt;br /&gt;
 &lt;br /&gt;
To add a DHCP server to FBSD you have to install the port. The best and most commonly used port for this purpose is the isc-dhcpd3 port. &lt;br /&gt;
The ISC-DHCP3 server supports three mechanisms for IP address allocation. In &amp;quot;automatic allocation&amp;quot;, DHCP assigns a permanent IP address to a client. In &amp;quot;dynamic allocation&amp;quot;, DHCP assigns an IP address to a client for a limited period of time (or until the client explicitly relinquishes the address). In &amp;quot;manual allocation&amp;quot;, a client's IP address is assigned by the network administrator, and DHCP is used simply to convey the assigned address to the client. Dynamic allocation is the only one of the three mechanisms that allows automatic reuse of am address that is no longer needed by the client to which it was assigned. A particular network will use one or more of these mechanisms, depending on the policies of the network administrator.&lt;br /&gt;
&lt;br /&gt;
For our purpose of a simple DHCP server that would fill the needs of the common FBSD user we are going to configure the DHCP server for &amp;quot;dynamic allocation&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How DHCP Works ==&lt;br /&gt;
&lt;br /&gt;
When the dhcpd daemon starts up at FBSD boot time, it broadcasts its presence through the LAN, then it sleeps and listens for broadcast requests for network configuration information from the LAN workstations. By default, it will listen on UDP port 67. When such a request is received, then the server will reply to the client machine on UDP port 68, providing the details required to connect to the network such as the IP address assigned to the workstation, subnet mask, default gateway and DNS servers names or IP addresses. Also included with this reply is a length of time for which this information can be used by that particular client. This is known as a DHCP &amp;quot;lease&amp;quot; and a new lease must be acquired by the client when it expires. The length of time for which a lease is valid is decided by the administrator of the DHCP server. The DHCP server keeps a database of leases it has issued in /var/db/dhcpd.leases File. This file is written as a log and can be edited. See man dhcpd.leases which gives a slightly longer description. DHCP clients can obtain a great deal of information from the server. An exhaustive list may be found in man dhcp-options &amp;amp; man dhcpd after DHCP is installed.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== DHCP Configuration Instructions ==&lt;br /&gt;
&lt;br /&gt;
To install the DHCP software, use the FBSD dhcp package using the following command &lt;br /&gt;
&lt;br /&gt;
pkg_add -rv isc-dhcp3-server&lt;br /&gt;
&lt;br /&gt;
To start the DHCPD server at boot time add the following statements in the /etc/rc.conf file.&lt;br /&gt;
&lt;br /&gt;
ee /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
dhcpd_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
dhcpd_conf=&amp;quot;/usr/local/etc/dhcpd.conf&amp;quot;&lt;br /&gt;
dhcpd_ifaces=&amp;quot;xl0&amp;quot;&lt;br /&gt;
dhcpd_flags=&amp;quot;-q&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The -q option will turn off the copyright banner that displays during the FBSD boot up and in the DHCP log every time a broadcast is issued by the DHCP daemon or when a request is received from a workstation DHCP client.&lt;br /&gt;
&lt;br /&gt;
The dc0 is to be replaced with the interface name of the LAN NIC you want DHCP service on from your gateway/firewall FBSD system. &lt;br /&gt;
&lt;br /&gt;
The dhcpd.conf file is delivered as a sample file so you have to make a copy of it without its sample suffix. It contains a lot of comments and commented out statement examples which you can comment out or delete. Edit the main DHCP configuration file and make it look like this.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
cp dhcpd.conf.sample dhcpd.conf&lt;br /&gt;
&lt;br /&gt;
ee dhcpd.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
option domain-name &amp;quot;fbsdjones.com&amp;quot;;&lt;br /&gt;
option domain-name-servers 208.206.15.11, 208.206.15.12;&lt;br /&gt;
# 600=10min, 7200=2 hours, 86400=1 day, 604800=1 week, 2592000=30 days&lt;br /&gt;
default-lease-time 86400;&lt;br /&gt;
max-lease-time 604800;&lt;br /&gt;
authoritative;&lt;br /&gt;
ddns-update-style none;&lt;br /&gt;
log-facility local1;&lt;br /&gt;
# No service will be given on this subnet, but declaring it helps the &lt;br /&gt;
# DHCP server to understand the network topology.&lt;br /&gt;
subnet 10.152.187.0 netmask 255.255.255.0 { }&lt;br /&gt;
&lt;br /&gt;
# This is the fbsdjones.com subnet declaration.&lt;br /&gt;
# Max of 6 pc on LAN 10.0.10.1 - 10.0.10.6&lt;br /&gt;
# 10.0.10.2 is the IP address of the Nic card in FBSD&lt;br /&gt;
# 10.0.10.7 is the broadcast IP address &lt;br /&gt;
subnet 10.0.10.0 netmask 255.255.255.248 {&lt;br /&gt;
range 10.0.10.1 10.0.10.6;&lt;br /&gt;
option routers 10.0.10.2;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option domain-name &amp;quot;fbsdjones.com&amp;quot;; is the user selected domain name from the hostname=&amp;quot;gateway.fbsdjones.com&amp;quot; statement of /etc/rc.conf. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option domain-name-servers contains the DSN server's IP addresses of your ISP from /etc/resolv.conf nameserver statements which get populated automatically when you connect to your ISP. If you have your own private LAN domain DSN server, make it the first one in the list, and in that case you can use full domain names instead of IP address (such as dnslocal.fbsdjones.com, dsn1.isp-domain.com).&lt;br /&gt;
&lt;br /&gt;
The default-lease-time and max-lease-time have values in seconds to set the elapse period for these function. The values I show are good to go with.&lt;br /&gt;
&lt;br /&gt;
The authoritative; options tells the DHCP daemon server that it is the boss and is in control of issuing all the information to the LAN DHCP clients.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The ddns-update-style none; tells DHCP that there is no local LAN DSN server. If you have one, change this from none to interim. In the dhcpd.conf.sample you will see comments saying none and ad-hoc are the two options. This is no longer true for DHCP version 3.0. Ad-hoc has been deactivated and replaced with interim. See man dhcpd.conf for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The log-facility allows you to segregate the DHCP messages to a separate log for recording. You are going to use local1 for logging of DHCP server error messages;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
subnet 10.0.10.0 netmask 255.255.255.248 {&lt;br /&gt;
range 10.0.10.1 10.0.10.6;&lt;br /&gt;
option routers 10.0.10.2; }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The subnet 10.0.10.0 netmask 255.255.255.248 statement declares the maximum subnet IP address range. In this case the last three digits in the netmask, 248 determines the range. This means a total of 8 IP addresses, 10.0.10.0 through 10.0.10.7 are allocated as the subnet range. 10.0.10.0 and 10.0.10.7 are reserved for the broadcast process. &lt;br /&gt;
&lt;br /&gt;
The range 10.0.10.1 10.0.10.6; is saying this range of IP addresses makes up the pool of addresses that are to be used for dynamic IP allocation to DHCP clients. It's a small home LAN with only two MS/Windows boxes and a single FBSD box on it now. That can grow to six machines without making any changes to this statement group. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The option routers 10.0.10.2 statement is a bit miss-leading. What this is referring to is the NIC in the FBSD box the DHCP server runs on and the LAN being configured is cabled to. In our case the NIC has an IP address of 10.0.10.2 which is specified in /etc/rc.conf by the ifconfig_dc0=&amp;quot;inet 10.0.10.2 netmask 255.255.255.248&amp;quot; statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The principle behind bitmasks and netmasks is simple, but often confusing to new users as it requires knowledge of binary numbers. For a quick reference, the following table illustrates what network ranges are indicated by the corresponding bitmasks/netmasks up to a default class C netmask. &lt;br /&gt;
&lt;br /&gt;
Bitmask   Netmask          Total IP's /  Usable IP's&lt;br /&gt;
  32      255.255.255.255         1              1&lt;br /&gt;
  31      255.255.255.254         2              1&lt;br /&gt;
  30      255.255.255.252         4              2&lt;br /&gt;
  29      255.255.255.248         8              6&lt;br /&gt;
  28      255.255.255.240        16             14&lt;br /&gt;
  27      255.255.255.224        32             30&lt;br /&gt;
  26      255.255.255.192        64             62&lt;br /&gt;
  25      255.255.255.128       128            126&lt;br /&gt;
  24      255.255.255.0         256            254&lt;br /&gt;
  22      255.255.192.0       16320          16318&lt;br /&gt;
  20      255.255.128.0       32768          32766&lt;br /&gt;
  16      255.255.0.0         65536          65534&lt;br /&gt;
  12      255.128.0.0   8.388608+e6    8.388606+e6 &lt;br /&gt;
   8      255.0.0.0           256^3      (256^3)-2&lt;br /&gt;
   0      0.0.0.0  (all IP's) 256^4       (256^4)-2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, there is a definite pattern. The number of total IP's always doubles, and the number of usable IP's is always total - 2. This is because for every IP network/subnet there are two IP's reserved for the network and broadcast addresses. The netmask's last octet starts at 255 and constantly decreases by multiples of 2, while the bitmask decreases by multiples of 1, because in binary, each shift over to the left halves the number, not divides by ten like in the decimal number system. This same pattern goes for all possible netmasks and bitmasks. &lt;br /&gt;
&lt;br /&gt;
Since you told DHCPD to use local1 for logging in the dhcpd.conf configuration file above, you now have to complete the logging environment configuration by adding the following statement to /etc/syslog.conf. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
ee /etc/syslog.conf &lt;br /&gt;
&lt;br /&gt;
local1.notice         /var/log/dhcpd.log&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
This log file does not exist, so you must create it.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/dhcpd.log&lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Now you must set up log rotation. Add this statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ee /etc/newsyslog.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/var/log/dhcp.log          600 3 100 * B&lt;br /&gt;
&lt;br /&gt;
You can change the log rotation triggers to whatever you want.&lt;br /&gt;
See man newsyslog for info on what the trigger values mean.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The DHCPD daemon has a start up script located at /usr/local/etc/rc.d/&lt;br /&gt;
&lt;br /&gt;
This directory location is where FBSD looks for files that end in .sh and executes them at the end of the boot process to start the applications. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can administer the DHCPD server from the command line using&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/isc-dhcp.sh start &lt;br /&gt;
                                stop&lt;br /&gt;
                                restart&lt;br /&gt;
                                &lt;br /&gt;
Restart is used to reread dhcp.conf file after making changes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now manually start DHCP by entering this on the command.&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/isc-dhcp.sh start &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Issue 'ps ax' command to see the DHCP daemon running in the active task list. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Testing the DHCPD Daemon &lt;br /&gt;
To test the DHCPD server you need a PC on the LAN. &lt;br /&gt;
&lt;br /&gt;
First let's check the LAN MS/Windows box network configuration. Click on the following buttons in this order. Start/settings/control panel/network/. Highlight TCP/IP and click on properties button. In the IP address tab the 'obtain IP address automatically' should be to only thing check marked. All the fields in the other tabs must be blank. If this is what you have use the cancel buttons to back yourself out. If you answer ok, you may have to have the windows install CDROM to update the network section.&lt;br /&gt;
&lt;br /&gt;
Windows 98, 2000, and ME have a program c:/windows/winipcfg.exe which will show you the DHCP info it's using. Start the winipcfg program by clicking on start, run, and type c:/windows/winipcfg.exe into the run window and then hit the OK button. Click on the more info button to see everything. You should be able to comprehend what you see back to the dhcpd.conf options as explained above. Click on the 'renew all' button to acquire a new DHCP lease. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FBSD as a DHCP Client ==&lt;br /&gt;
 &lt;br /&gt;
The isc-dhcp3 port comes with a client. I am not going to cover the isc-dhcp3 port client configuration process, because FBSD comes with a DHCP client built into the basic FBSD system. &lt;br /&gt;
&lt;br /&gt;
To activate the built in dhcp client on a FBSD LAN PC, edit /etc/rc.conf and add the following statement to tell FBSD what interface the client DHCP should use:&lt;br /&gt;
&lt;br /&gt;
ee /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ifconfig_dc0=&amp;quot;DHCP&amp;quot;     # Where dc0 is the FBSD Nic card interface name.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That's it, configuration complete. Reboot to activate your changes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Local_Area_Network_(LAN)</id>
		<title>Local Area Network (LAN)</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Local_Area_Network_(LAN)"/>
				<updated>2012-08-13T23:34:57Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: Local Area Network (LAN)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Setting up Local Area Network (LAN) ==&lt;br /&gt;
&lt;br /&gt;
A local area network (LAN) is a group of computers and associated devices that share a common communications line or wireless link and typically share the resources of a single processor or server within a small geographic area (for example, within an office building). Usually, the server has applications and data storage that are shared in common by multiple LAN computer users. A local area network may serve as few as two or three users (for example, in a home network) or as many as thousands of users (for example, in an FDDI network). Typically, a suite of application programs can be kept on the LAN server. Users who need an application frequently can download it once and then run it from their local hard disk. A user can share files with others at the LAN server. &lt;br /&gt;
&lt;br /&gt;
There are many technical limitations and options to how a LAN is configured depending on if you are a non-commercial user or commercial user and how many of the MS/Windows and/or Unix network server sharing facilities you want to enable. &lt;br /&gt;
&lt;br /&gt;
Basically what determines if you are commercial user or not is how you are known to the public Internet. A commercial user has a permanent, dedicated, high-speed leased Internet line connecting them to their ISP and one or more static IP addresses assigned by their ISP. A static IP address is always the same number; it never changes between logins to the ISP. They have an official registered domain name that points to one of the static IP address which points to their PC that is acting as their gateway. If the commercial user pays for a large block of static IP addresses then they can use these IP address for the computers on the LAN and not need to use NAT (network address translation). Their email will arrive at the gateway PC and is processed by their mail server directly. They do not use their ISP to receive and hold their email for them.&lt;br /&gt;
&lt;br /&gt;
An non-commercial user, like the home user, uses a phone line dial in login to their ISP on a limited speed connection or has a 24/7 cable or DSL high speed connection and gets assigned a single dynamic IP address which changes every time they login. Their ISP receives and holds all their email for them. The only way a public Internet user can find them is if that know the dynamic IP address currently in use by them. From the ISP viewpoint a non-commercial user uses a very small amount of its overall resources and so charges much less for a single user account.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FBSD system that is acting as the gateway can also be configured to provide different levels of network sharing depending on what kind of operating systems are running on the PCs connected to the LAN. For Unix-like operating systems NFS provides network file and device sharing, while the FBSD port application Samba does the same thing for MS/Windows PCs on the LAN. These facilities, NFS and Samba are not covered in this guide as they are more applicable to commercial users who have large LANs.&lt;br /&gt;
&lt;br /&gt;
See this link for Samba details http://us1.samba.org/samba/samba.html&lt;br /&gt;
&lt;br /&gt;
See the following links for details on FBSD NFS:&lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/network-nfs.html&lt;br /&gt;
&lt;br /&gt;
http://www.onlamp.com/pub/a/bsd/2000/07/26/FreeBSD_Basics.html&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Home User LANs ==&lt;br /&gt;
&lt;br /&gt;
Normally each family member would have to have their own phone line and unique ISP account to connect all of the family PCs to the Internet simultaneously. This is a costly way of doing this. The alternative is to have a single FBSD system gateway connect to an ISP and then network the other family members PC's behind the gateway using private IP addresses and NAT (Network Addressing Translation) so everything leaving the gateway system looks like it came from the single dynamic IP address assigned by the ISP. Your ISP can not tell if the packet passing through them has been NATed or not.&lt;br /&gt;
&lt;br /&gt;
Installer Note: When you sign up for service with your ISP you have to sign a user agreement that basically says you are not allowed to do NAT on your PC or run email services or web servers. If you are caught, it's grounds for them to terminate your account. Never tell your ISP tech support people what you are doing. Most ISP's leave open all the ports except the port used by an email server, which they block. More recently some ISPs have started to also block the web server port number.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another simple to configure facility is an anonymous FTP server on the FBSD gateway so LAN users can post files there that they want to pass to other LAN users. This allows them to pass large files between LAN users.&lt;br /&gt;
&lt;br /&gt;
The LAN can be populated with both MS/Windows boxes and FBSD boxes and not cause any problems. ISP's usually allow 5 email address per dial in account. Each family member can have their own email address and using the email client on their PC get their email directly from the ISP email server. Or you can run a task on the FBSD gateway box to download the email from the ISP account on a recurring schedule and store it in the FBSD built in email server called sendmail, and then have all the LAN users get their email from the sendmail server without having the FBSD gateway connected to the Internet.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Topography of a LAN&lt;br /&gt;
        __________         ________             _____ &lt;br /&gt;
       |          |       |        |           |     | &lt;br /&gt;
       |  FBSD/GW |       | switch |&amp;lt;---------&amp;gt;| LAN |&lt;br /&gt;
       |          |       | or     |           | PC1 |    _____&lt;br /&gt;
       |      NIC |&amp;lt;-----&amp;gt;| hub    |&amp;lt;--|       |_____|   |     |&lt;br /&gt;
       |__________|       |________|   |                 | LAN |&lt;br /&gt;
                                       |&amp;lt;---------------&amp;gt;| PC2 |&lt;br /&gt;
                                                         |_____| &lt;br /&gt;
                                                             &lt;br /&gt;
                                                            &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The above diagram shows a simple single LAN circuit. Your FBSD gateway box needs a NIC for each separate LAN circuit. Each circuit must use a unique IP address subnet. You cable the LAN NIC from the back of the FBSD gateway PC to a network switch or hub. A small cheap switch normally has 5 plugs. One plug for each PC on the LAN including the FBSD gateway. You run a cable from the switch to the NIC of each PC you want on the LAN. A LAN circuit can handle many PC's and many downstream switches as long as the max distance of the cable is not exceeded. To add more LAN users you create another circuit by adding another NIC in the FBSD box connected to another switch which has more LAN PC's connected to it. Please note, this is a very simplified LAN description and layout, but is adequate for basic understanding of how the physical parts of the LAN are cabled together.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For the home user with just two PCs, you can cable your FBSD LAN NIC directly to the other PC's NIC with a special crossover cable.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Installing the LAN ==&lt;br /&gt;
&lt;br /&gt;
Your PC should have two NIC already installed. &lt;br /&gt;
During the boot of FBSD review the /var/run/dmesg.boot log to verify that your PCI NIC were found. &lt;br /&gt;
&lt;br /&gt;
This is what you are looking for. This means that FBSD GENERIC kernel found your NIC. The dc0 name will be different depending on the manufacture of your NIC.&lt;br /&gt;
&lt;br /&gt;
dc0: &amp;lt;Macronix 98715/98715A 10/100BaseTX&amp;gt; port 0xdc00-0xdcff mem &lt;br /&gt;
0xe3000000-0xe30000ff irq 3 at device 19.0 on pci0&lt;br /&gt;
dc0: Ethernet address: 00:80:c6:f2:2e:3b&lt;br /&gt;
miibus0: &amp;lt;MII bus&amp;gt; on dc0&amp;gt;&lt;br /&gt;
dcphy0: &amp;lt;Intel 21143 NWAY media interface&amp;gt; on miibus0&lt;br /&gt;
dcphy0: 10baseT, 10baseT-FDX, 100baseTX, 100baseTX-FDX, auto&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
dc0 is the NIC FBSD internal interface name.&lt;br /&gt;
&lt;br /&gt;
The generic kernel contains device statements for most of the NIC currently on the market. If the /var/run/dmesg.boot log shows your NIC as&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
pci0: &amp;lt;unknown card&amp;gt; (vendor=0x1274, dev=0x5000) at 19.0&lt;br /&gt;
&lt;br /&gt;
or no message to indicate the probe of the BOIS found any PCI devices, then you may have an older BOIS on your PC which does not handle PCI cards very well. On older (IE: pre Y2K) PC BOIS, it's very common for the system probe process of the bio's to be unable to find one or more PCI cards. If this happens to you, you have to do some research to determine the problem.&lt;br /&gt;
&lt;br /&gt;
Try the pciconf -lv command to see if it gives you any useful info. Then review the GENERIC source at /usr/src/sys/i386/conf/GENERIC to see if it contains any device statement comments about your NIC based on the manufacturer or chips used. If you do find a device statement in the GENERIC source for your NIC, then add this statement to your kernel source and recompile your kernel.&lt;br /&gt;
&lt;br /&gt;
device pun&lt;br /&gt;
&lt;br /&gt;
This device has additional code to probe your system's BIOS using different methods which in most cases results in your PCI NIC being found.&lt;br /&gt;
&lt;br /&gt;
If the review of the GENERIC kernel source produces no results, them review the kernel source file named LINT at /usr/src/sys/i386/conf/ for comments that describe your NIC by manufacturer name or chips used. Copy the appropriate device statements to the GENERIC kernel source file and then follow the instructions at Kernel Customizing. You will have to create a custom kernel from the GENERIC source including the device statement from the LINT source.&lt;br /&gt;
&lt;br /&gt;
If you find no kernel device statements for your NIC, then it's not supported and you have to get one that is.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== LAN private IP address ==&lt;br /&gt;
&lt;br /&gt;
There are ranges of special IP addresses reserved for use on private LANs. These special IP address ranges are non-routable on the public Internet. They are listed in the /etc/hosts file.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP address ranges for&lt;br /&gt;
private networks which will never be connected to the Internet:&lt;br /&gt;
&lt;br /&gt;
10.0.0.0    - 10.255.255.255 &lt;br /&gt;
172.16.0.0  - 172.31.255.255&lt;br /&gt;
192.168.0.0 - 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
These can also be written as&lt;br /&gt;
10.0.0.0/8 &lt;br /&gt;
172.16.0.0/12 &lt;br /&gt;
192.168.0.0/16&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
To communicate with the LAN PC's the FBSD system needs to know what the IP address range of the PC's on the LAN is and the LAN PC's needs to be configured with LAN network information so they know how to perform their part in the communication process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are two ways to accomplish this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The manually way by hand, or&lt;br /&gt;
&lt;br /&gt;
The automatic way using the FBSD port application DHCP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually Configuring the Gateway host ==&lt;br /&gt;
 &lt;br /&gt;
Before you can manually configure each PC on the LAN by hand, you first have to collect some information from your FBSD gateway box. It's assumed you already have your gateway PC connection to the public Internet already working. &lt;br /&gt;
&lt;br /&gt;
The configuration file /etc/resolv.conf is automatically populated with the IP address of your ISP's primary and secondary domain name servers every time you log in to your ISP. Write down these IP addresses you will need them to configure your LAN PCs.&lt;br /&gt;
&lt;br /&gt;
Now you decide on the private IP address range to use for your LAN. This guide uses a very small portion of the 10.0.0.0/8 range for the private Lan, which is 10.0.10.0/29. This gives 10.0.10.0 through 10.0.10.7. The usable portion of the range is 10.0.10.1 through 10.0.10.6, the 10.0.10.0 and 10.0.10.7 is the reserved pair for broadcasting. &lt;br /&gt;
&lt;br /&gt;
The IP address of the NIC in the FBSD gateway will be 10.0.10.2. The IP address of the first LAN PC to be manually configured will be 10.0.10.3.&lt;br /&gt;
&lt;br /&gt;
On the FBSD gateway system add these two statements to /etc/rc.conf to manually assign the FBSD LAN NIC an IP address and tell FBSD to act as a gateway for the LAN.&lt;br /&gt;
&lt;br /&gt;
ifconfig_dc0=&amp;quot;inet 10.0.10.2 netmask 255.255.255.248&amp;quot;&lt;br /&gt;
&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dc0 is the gateway interface name of the NIC the LAN is cabled to.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually configuring LAN FBSD PC ==&lt;br /&gt;
&lt;br /&gt;
For a FBSD workstation PC on the LAN, add these statements to /etc/rc.conf to manually assign the FBSD LAN NIC an IP address. Be sure to change dc0 to the interface name of the Nic card in the FBSD LAN PC.&lt;br /&gt;
&lt;br /&gt;
ifconfig_dc0=&amp;quot;inet 10.0.10.3 netmask 255.255.255.248&amp;quot;&lt;br /&gt;
&lt;br /&gt;
defaultrouter=&amp;quot;10.0.10.2&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Copy the FBSD gateway /etc/resolv.conf file to the FBSD LAN PC, replacing the one that's there. Or edit the FBSD LAN PC's /etc/resolv.conf so it's the same as the one from the FBSD gateway. Reboot system to enable your changes.&lt;br /&gt;
&lt;br /&gt;
To test, ping the gateway server:&lt;br /&gt;
&lt;br /&gt;
ping -c 4 10.0.10.2&lt;br /&gt;
&lt;br /&gt;
Then test DNS by pinging:&lt;br /&gt;
&lt;br /&gt;
ping -c 4 freebsd.org&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manually config MS/Windows LAN PC ==&lt;br /&gt;
 &lt;br /&gt;
This procedure has been tested on MS/Windows 98, ME and XP.&lt;br /&gt;
&lt;br /&gt;
Click on start, settings, control panel, networking. In the window the installed network components are displayed. Scroll through them and click to highlight the TCP/IP line for the NIC you are going to use to connect this box to your LAN. When it's highlighted, the properties button below the window becomes enabled. Click on the properties button and a window pops up which is where you manually configure the NIC TCP/IP network settings.&lt;br /&gt;
&lt;br /&gt;
Under the IP address tab, click on specify IP address. For IP address enter 10.0.10.4&lt;br /&gt;
&lt;br /&gt;
Under the gateway tab, new gateway window enter the IP address of the FBSD gateway&lt;br /&gt;
&lt;br /&gt;
10.0.10.2 and click the add button.&lt;br /&gt;
&lt;br /&gt;
Under the DNS configuration tab, click on enable DNS. In the DNS server search order window enter the first of the two IP addresses you got from the FBSD gateway /etc/resolv.conf file. Click on the add button, then do same thing over again for the second IP address. When you're finished click on the OK button at the bottom of the pop up window, and click OK again. The system will reboot to activate your changes.&lt;br /&gt;
&lt;br /&gt;
To test, click on start, run.&lt;br /&gt;
&lt;br /&gt;
Enter C:\windows\command.com&lt;br /&gt;
&lt;br /&gt;
When a native DOS window opens, ping the gateway server: &lt;br /&gt;
&lt;br /&gt;
Ping 10.0.10.2&lt;br /&gt;
&lt;br /&gt;
Then test DNS:&lt;br /&gt;
&lt;br /&gt;
ping freebsd.org&lt;br /&gt;
&lt;br /&gt;
The DNS servers will convert this domain name to an IP address and then send four pings to it. When this has completed, enter exit to leave native DOS mode.&lt;br /&gt;
&lt;br /&gt;
For each additional FBSD or MS/Windows LAN PCs you want to add, just increment the last digit of the last assigned IP address by 1. You may have to keep a log book so you know what LAN IP addresses you have assigned. All LAN PCs connected to the FBSD gateway 10.0.10.2 NIC have to use the same IP address sub-net, (IE: 10.0.10.x) where in this example x can be 1 through 6.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Example_of_configuring_OpenSSH</id>
		<title>Example of configuring OpenSSH</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Example_of_configuring_OpenSSH"/>
				<updated>2012-08-13T23:20:04Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* WinSCP3 Setup Steps */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Example of configuring OpenSSH ==&lt;br /&gt;
Normally, when using native telnet, rlogin, or ftp your login ID, password and tramsmitted data is sent over the network in an clear, un-encrypted form. Network sniffers anywhere in between the client and server can steal your Login information and/or data transferred in your session. &lt;br /&gt;
&lt;br /&gt;
OpenSSH encrypts all traffic (including login ID and passwords) to effectively eliminate eavesdropping, connection hijacking, and other network-level attacks. Additionally, OpenSSH provides a myriad of secure tunneling capabilities, as well as a variety of authentication methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example Environment Description ==&lt;br /&gt;
&lt;br /&gt;
In this example we have a FreeBSD system which we will call the host. This is the system that remote users want to have login access to.&lt;br /&gt;
&lt;br /&gt;
We have an Remote FreeBSD system which is located some where on the public internet, we will call this the FBSD-client. &lt;br /&gt;
&lt;br /&gt;
We also have an Remote MS/windows system which is located some where on the public internet, we will call this the Win-client.&lt;br /&gt;
&lt;br /&gt;
This example details the encrypted Host/Client key with passphrase authentication method. This method gives the maximum protection possible utilizing ssh. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Host Setup Steps ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.  Edit /etc/rc.conf and add this statement&lt;br /&gt;
         sshd_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
     Make sure your firewall allows port 22 in from the public internet.&lt;br /&gt;
     Reboot your system to activate sshd and login as root.&lt;br /&gt;
     If this is your first time booting with sshd you will have to&lt;br /&gt;
     create the host keys.&lt;br /&gt;
     sshd will show you this on the first sshd boot only.&lt;br /&gt;
&lt;br /&gt;
    ====================================================&lt;br /&gt;
    Type a full screen full of random junk to unblock&lt;br /&gt;
    it and remember to finish with &amp;lt;enter&amp;gt;. This will&lt;br /&gt;
    timeout in 300 seconds, but waiting for&lt;br /&gt;
    the timeout without typing junk may make the&lt;br /&gt;
    entropy source deliver predictable output.&lt;br /&gt;
    Just hit &amp;lt;enter&amp;gt; for fast+insecure startup.&lt;br /&gt;
    ====================================================&lt;br /&gt;
    kern.random.sys.seeded: 1 -&amp;gt; 0&lt;br /&gt;
    qkcir83,2jsn40pl722jjbqok ---- this is the example junk entered&lt;br /&gt;
    Generating public/private rsa1 key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    ed:5d:97:dc:49:98:36:66:fc root@domainname&lt;br /&gt;
    Generating public/private dsa key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_dsa_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    67:e7:90:04:0e:27:2e:d2:97:6a root@a1poweruser.com&lt;br /&gt;
    Generating public/private rsa key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_rsa_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    96:db:50:5c:9e:69:88:26:28:54 root@domainname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2.  If you do a &amp;quot;ps ax&amp;quot; command you will see sshd as one &lt;br /&gt;
    of the running tasks.&lt;br /&gt;
&lt;br /&gt;
3.  Using adduser or pw command create a normal user account.&lt;br /&gt;
    For this example we will use bob as the host user account name.&lt;br /&gt;
&lt;br /&gt;
4.  Hit alt/F2 at same time to open second session and login using bob.&lt;br /&gt;
&lt;br /&gt;
    Run this command ssh-keygen -t rsa&lt;br /&gt;
&lt;br /&gt;
    Just hit enter to take default location and file name&lt;br /&gt;
    No need to enter a pass phrase for the host user here,&lt;br /&gt;
    just hit enter 2 times.&lt;br /&gt;
    This is what you will see&lt;br /&gt;
&lt;br /&gt;
    Generating public/private rsa key pair.&lt;br /&gt;
    Enter file in which to save the key (/bob/.ssh/id_rsa):&lt;br /&gt;
    Created directory '/bob/.ssh'.&lt;br /&gt;
    Enter passphrase (empty for no passphrase):&lt;br /&gt;
    Enter same passphrase again:&lt;br /&gt;
    Your identification has been saved in /bob/.ssh/id_rsa.&lt;br /&gt;
    Your public key has been saved in /bob/.ssh/id_rsa.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    e7:e6:8f:d3:b1:b4:08:27:09:d2 bob@domainname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
5.  If you want to use the Host 'root' account to initiate a ssh&lt;br /&gt;
    session with an remote system you have to perform step 4 above.&lt;br /&gt;
    If you want to do remote ssh login to this Host as user ‘root’, you&lt;br /&gt;
    have to edit /etc/ssh/sshd_config on the this Host and change;&lt;br /&gt;
&lt;br /&gt;
    #PermitRootLogin no  to  PermitRootLogin yes &lt;br /&gt;
&lt;br /&gt;
    Then /etc/rc.d/sshd reload  &lt;br /&gt;
&lt;br /&gt;
    to make sshd task reread it’s sshd_config file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== FBSD-client Setup Steps ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1. Using adduser or pw command create a normal user account.&lt;br /&gt;
   For this example we will use remotetom as the user account name.&lt;br /&gt;
&lt;br /&gt;
2. Login using remotetom.&lt;br /&gt;
   Run this command ssh-keygen -t rsa&lt;br /&gt;
&lt;br /&gt;
3. Just hit enter to take default location and file name.&lt;br /&gt;
   At the &amp;quot;Enter a passphrase prompt&amp;quot; [enter one and write it down, &lt;br /&gt;
   because it will be needed for ssh login to the host].&lt;br /&gt;
   This is what you will see&lt;br /&gt;
&lt;br /&gt;
   Generating public/private rsa key pair.&lt;br /&gt;
   Enter file in which to save the key (/remotetom/.ssh/id_rsa):&lt;br /&gt;
   Created directory '/remotetom/.ssh'.&lt;br /&gt;
   Enter passphrase (empty for no passphrase):&lt;br /&gt;
   Enter same passphrase again:&lt;br /&gt;
   Your identification has been saved in /remotetom/.ssh/id_rsa.&lt;br /&gt;
   Your public key has been saved in /remotetom/.ssh/id_rsa.pub.&lt;br /&gt;
   The key fingerprint is:&lt;br /&gt;
   e7:e6:8f:d3:b1:b4:08:27:09:56:de:d2 tom@domainname &lt;br /&gt;
&lt;br /&gt;
4. The Public key file you just created /remotetom/.ssh/id_rsa.pub &lt;br /&gt;
   has to be sent to the Host system. On the host system rename it&lt;br /&gt;
   to authorized_keys2 and put it into the home directory of the &lt;br /&gt;
   user setup earlier. In this case ~/bob/.ssh/authorized_keys2. &lt;br /&gt;
&lt;br /&gt;
5. To ssh to the host, enter this ssh hostname or ssh host-ip-address&lt;br /&gt;
  The first time you ssh to the Host you will get these messages.&lt;br /&gt;
  Answer yes if you are sure this first connection is with your&lt;br /&gt;
  targeted host. Enter exit to terminate session.&lt;br /&gt;
&lt;br /&gt;
   # /home/remotetom/.ssh &amp;gt;ssh 10.0.10.2&lt;br /&gt;
   The authenticity of host '10.0.10.2 (10.0.10.2)' can't be established.&lt;br /&gt;
   DSA key fingerprint is 04:0e:27:2e:d2:97:9d:7b:62:7e:c9:32:6a.&lt;br /&gt;
   Are you sure you want to continue connecting (yes/no)? yes&lt;br /&gt;
   Warning: Permanently added '10.0.10.2' (DSA) to list of known hosts.&lt;br /&gt;
   Enter passphrase for key /remotetom/.ssh/id_rsa&lt;br /&gt;
&lt;br /&gt;
   This is where you enter the pass phrase you entered while running &lt;br /&gt;
   ssh-keygen on this remote client. (remotetom)&lt;br /&gt;
&lt;br /&gt;
6. You are now logged into the Host account bob.&lt;br /&gt;
   Every time you ssh login again you will just get this prompt&lt;br /&gt;
&lt;br /&gt;
   Enter passphrase for key /remotetom/.ssh/id_rsa&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
7. At this point the Host user bob can login from Host console, or using telnet from a local LAN machine, or from the public internet if your host firewall has port 23 open. If this user is to be only allowed remote ssh client access you can use vipw command to edit the system’s master password file and star out his encrypted password. This will deny login from anywhere except remotebob&lt;br /&gt;
&lt;br /&gt;
bob:$1$FXF.CEpf:1001:31::0:0:remote ssh only #:/home/bob:/bin/csh&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
change it to look like this&lt;br /&gt;
&lt;br /&gt;
bob:*:1001:31::0:0:remote ssh only #:/home/bob:/bin/csh&lt;br /&gt;
&lt;br /&gt;
8. At this point, if remotebob is a notebook pc he can ssh login from any place in the world. If remotebob is always working from home using ip address 192.168.1.32 you can add another level of security by restricting his login to come from that ip address. Do that by adding this statement to the Host /etc/ssh/sshd_config file&lt;br /&gt;
&lt;br /&gt;
AllowUsers bob@192.168.1.32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using Midnight Commander for SFTP ==&lt;br /&gt;
&lt;br /&gt;
So far the FBSD-client setup steps above have described how to use native SSH for creating the SSH keys and logging in to the FreeBSD SSHD host. Using ssh's sftp is a command line environment. Meaning you have to know all the FTP commands to navigate around the local and remote directories plus the get and put commands to really move the files between PC's. This can really be a problem for the infrequent ftp user. An command line (I.E. not X) GUI program would make this sftp directory navigation much easier. &lt;br /&gt;
&lt;br /&gt;
We are in luck. The ports system contains 'Midnight commander' an command line GUI program designed after the MS/Windows Norton Commander. It's panel has a 2 up display containing the source and target directory trees. It's normal usage is for navigating around your File system layout. But it has built in functions which allow it to do native ftp and ssh sftp between it self and the remote ftpd or sshd host. &lt;br /&gt;
&lt;br /&gt;
The current version of MC 'IE. Midnight commander' on FreeBSD 6.0 is MC 4.6.0. There is an newer version MC 4.6.1.3 in the ports which may fix the ssh login problem described later. &lt;br /&gt;
&lt;br /&gt;
You still have to run steps 1 through 5 above to create the SSH keys and moved it to the host system and then test ssh sftp login. &lt;br /&gt;
&lt;br /&gt;
After installing MC from the ports system or package system.&lt;br /&gt;
You start it be entering  mc  on the command line.&lt;br /&gt;
This brings up the GUI panel. There are 2 ways to use MC for ftp or sftp.&lt;br /&gt;
&lt;br /&gt;
One way uses the different menu panels and allows you to save the login information as a bookmark. Use F9 to active the &lt;br /&gt;
&amp;quot;Left file command options right&amp;quot; menus at the top of the MC panel. &lt;br /&gt;
The left &amp;amp; right pull down menu option have the FTP link &amp;amp; SH link option.&lt;br /&gt;
&lt;br /&gt;
The second way is the short cut method. You just enter an &amp;quot;cd&amp;quot; change directory command on the MC command line.&lt;br /&gt;
&lt;br /&gt;
cd /#sh:user@remote Host&lt;br /&gt;
&lt;br /&gt;
user = the name of the user account on the host. In this example (bob) &lt;br /&gt;
remote host = the FQDN or IP address of the host &lt;br /&gt;
&lt;br /&gt;
The MC command line at the bottom of the panel asks for the SSH pass phrase.&lt;br /&gt;
After entering the pass phrase, the remote hosts directory tree is displayed with a heading of the /#sh: command used to launched it.&lt;br /&gt;
&lt;br /&gt;
After entering the pass phrase the MC command line does not clear and ends up containing some left over parts of the SSH message. This is a bug. &lt;br /&gt;
&lt;br /&gt;
Holding down the CTL key while pressing the keyboard letter C and O keys at the same time, 2 times in a role will clear the MC command line.&lt;br /&gt;
&lt;br /&gt;
When your finished doing SSH sftp through MC, terminate the MC SSH session with the remote host by hitting F10 key.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Win-client Setup Steps ==&lt;br /&gt;
&lt;br /&gt;
There are two MS/Windows SSH client programs. The &amp;quot;Putty&amp;quot; program which provides basic SSH access for Telnet style logins and native FTP access. The &amp;quot;WinSCP3&amp;quot; program which is built on &amp;quot;putty&amp;quot; underling code that provides a Norton commander GUI style SFTP access. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Putty Setup Steps&lt;br /&gt;
1. You have to get windows putty program from&lt;br /&gt;
   http://www.chiark.greenend.org.uk/~sgtatham/putty/&lt;br /&gt;
   Download the current windows installer program.&lt;br /&gt;
   Currently that’s putty-0.58-installer.exe&lt;br /&gt;
&lt;br /&gt;
2. Run the putty-0.58-installer.exe and take all the defaults.&lt;br /&gt;
&lt;br /&gt;
3. Start/programs/Putty/Puttygen&lt;br /&gt;
   This will create your private and public ssh keys&lt;br /&gt;
   At the bottom of the window select ssh2-rsa&lt;br /&gt;
   Click on generate button.&lt;br /&gt;
   After keys are created then enter your pass phrase.&lt;br /&gt;
   Then click on ‘save public key’ button.&lt;br /&gt;
   Save it to c:/programs/putty/putty_public_rsa_key&lt;br /&gt;
   Then click on ‘save private key’ button.&lt;br /&gt;
   Save it to c:/programs/putty/putty_private_rsa_key.ppk&lt;br /&gt;
   In the window area labeled&lt;br /&gt;
   &amp;quot;Public key for pasting into Open SSH authorized_key file&amp;quot;&lt;br /&gt;
   Highlight that content and past it into a notepad text&lt;br /&gt;
   file named win_authorized_keys2.txt&lt;br /&gt;
&lt;br /&gt;
4. The win_authorized_keys2.txt file has to be sent to the&lt;br /&gt;
   Host system and put in the ~/home/bob/.ssh/ directory.&lt;br /&gt;
   In this example both the FBSD-client &amp;amp; the WIN-client will&lt;br /&gt;
   both use the same Host user account to ssh login to.&lt;br /&gt;
   Since the ~/home/bob/.ssh/authorized_keys2 already contains the&lt;br /&gt;
   private key for the FBSD-client you have to append the&lt;br /&gt;
   win_authorized_keys2.txt content to it.&lt;br /&gt;
&lt;br /&gt;
   cat win_authorized_keys2.txt &amp;gt;&amp;gt; authorized_keys2&lt;br /&gt;
&lt;br /&gt;
   If bob is only to be used by WIN-client then just&lt;br /&gt;
&lt;br /&gt;
   cp win_authorized_keys2.txt authorized_keys2&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
5. start/programs/putty/putty to run it&lt;br /&gt;
   On the bottom of the window click on &amp;quot;never exit&amp;quot;&lt;br /&gt;
   Under protocol be sure ssh is clicked.&lt;br /&gt;
   On the left side click on ‘SSH’, then in the&lt;br /&gt;
   ‘preferred ssh protocol version’ click on ‘2 only’.&lt;br /&gt;
   On the left side click on ‘AUTH’, remove check mark from&lt;br /&gt;
   ‘Attempt keyboard-interactive auth (SSH-2).&lt;br /&gt;
   On the left side click on ‘Sessions’ to return to main login window.&lt;br /&gt;
   Enter you host name (IE; your official registered domain name of&lt;br /&gt;
   the Host box) or it’s IP address. Then type a name in the saved&lt;br /&gt;
   session window and click on save button. Now click on the open button&lt;br /&gt;
   at bottom of window to login to the host.&lt;br /&gt;
&lt;br /&gt;
   A ms/dos window will open up and you will receive ‘login as’ prompt &lt;br /&gt;
   Enter bob&lt;br /&gt;
   Then you get&lt;br /&gt;
   Authenticating with public key &amp;quot;rsa-key-20060209&amp;quot;&lt;br /&gt;
   Passphrase for key &amp;quot;rsa-key-20060209&amp;quot;:&lt;br /&gt;
   At this point enter the pass phrase you entered when you ran &lt;br /&gt;
   Start/programs/Putty/Puttygen to create the rsa keys on&lt;br /&gt;
   the client remotetom.&lt;br /&gt;
&lt;br /&gt;
6. You are now logged into you host box using SSH. You have all &lt;br /&gt;
   the privileges that host user bob has when you created him.&lt;br /&gt;
   Every time you start/programs/putty/putty highlight the name&lt;br /&gt;
   of your saved session and click on the load button. This will &lt;br /&gt;
   restore your saved session configuration, then click open to launch.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== WinSCP3 Setup Steps ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1. You have to get windows WinSCP3 program from&lt;br /&gt;
   http://winscp.net/eng/index.php&lt;br /&gt;
   Download the windows installer program.&lt;br /&gt;
   Currently that's winscp380setup.exe&lt;br /&gt;
&lt;br /&gt;
2. Run winscp380setup.exe and take all the defaults.&lt;br /&gt;
&lt;br /&gt;
3. Start/programs/WinSCP3/keytools/puttygen&lt;br /&gt;
   This will create your private and public ssh keys &lt;br /&gt;
&lt;br /&gt;
   At the bottom of the window select ssh2-rsa&lt;br /&gt;
   Click on generate button.&lt;br /&gt;
&lt;br /&gt;
   After keys are created then enter your pass phrase.&lt;br /&gt;
&lt;br /&gt;
   Then click on 'save public key' button.&lt;br /&gt;
   Save it to c:/Program Files/WinSCP3/putty_public_rsa_key&lt;br /&gt;
&lt;br /&gt;
   Then click on 'save private key' button.&lt;br /&gt;
   Save it to c:/Program Files/WinSCP3/putty_private_rsa_key.ppk&lt;br /&gt;
&lt;br /&gt;
   In the window area labeled &lt;br /&gt;
   &amp;quot;Public key for pasting into Open SSH authorized_key file&amp;quot; &lt;br /&gt;
   Highlight that content and past it into a notepad text file&lt;br /&gt;
   named win_authorized_keys2.txt&lt;br /&gt;
&lt;br /&gt;
4. The win_authorized_keys2.txt file has to be sent to the Host system &lt;br /&gt;
   and put in the ~/home/bob/.ssh/ directory. In this example both &lt;br /&gt;
   the FBSD-client &amp;amp; the WIN-client will both use the same Host user &lt;br /&gt;
   account to ssh login to. &lt;br /&gt;
&lt;br /&gt;
   Since the ~/home/bob/.ssh/authorized_keys2 already contains the&lt;br /&gt;
   private key for the FBSD-client you have to append the&lt;br /&gt;
   win_authorized_keys2.txt content to it.&lt;br /&gt;
&lt;br /&gt;
    cat win_authorized_keys2.txt &amp;gt;&amp;gt; authorized_keys2&lt;br /&gt;
&lt;br /&gt;
   If bob is to only be used by WIN-client then just &lt;br /&gt;
      cp win_authorized_keys2.txt authorized_keys2&lt;br /&gt;
&lt;br /&gt;
5. Start/programs/WinSCP3/WinSCP3&lt;br /&gt;
   In the window on the left, click on SSH&lt;br /&gt;
   Then for preferred ssh protocol version, click on &amp;quot;2 only&amp;quot;&lt;br /&gt;
&lt;br /&gt;
   In the window on the left, click on session&lt;br /&gt;
   Then fill in your host name FQDN or IP address.&lt;br /&gt;
   Then the user name of the host account you want to login to.&lt;br /&gt;
   Then fill in the location of for the rsa putty key created&lt;br /&gt;
   in step 3 above&lt;br /&gt;
   c:/Program Files/WinSCP3/putty_private_rsa_key.ppk&lt;br /&gt;
&lt;br /&gt;
   For protocol click on sftp &lt;br /&gt;
&lt;br /&gt;
   At the bottom of the panel click on the save button&lt;br /&gt;
   Accept the name in the pop up window or enter your own&lt;br /&gt;
   value and click OK.&lt;br /&gt;
&lt;br /&gt;
   The stored session panel opens, highlight the session name you want &lt;br /&gt;
   to run and click on the load button. This fills in the session panel&lt;br /&gt;
   with your saved configuration.&lt;br /&gt;
&lt;br /&gt;
   Click on login button to initiate your sftp session with the host.&lt;br /&gt;
   A pop up window will show asking you to enter the pass phrase you &lt;br /&gt;
   entered in &amp;quot;Step 3&amp;quot; above. Click on OK after entering you pass phrase.&lt;br /&gt;
&lt;br /&gt;
   The first time you SSH to the Host you will get these messages. &lt;br /&gt;
   Answer yes if you are sure this first connection is with your&lt;br /&gt;
   targeted host.&lt;br /&gt;
&lt;br /&gt;
   The authenticity of host '10.0.10.2 (10.0.10.2)' can't be established.&lt;br /&gt;
   DSA key fingerprint is 67:e7:90:04:0e:27:2e:d2.&lt;br /&gt;
   Are you sure you want to continue connecting (yes/no)? yes&lt;br /&gt;
   Warning: Permanently added 10.0.10.2 (DSA) to the list of known hosts.&lt;br /&gt;
&lt;br /&gt;
   The GUI Norton commander style panel will display.&lt;br /&gt;
   Left side shows the client's directory and the &lt;br /&gt;
   right side shows the Host user accounts directory &lt;br /&gt;
&lt;br /&gt;
   Every time you start/programs/WinSCP3/WinSCP3 highlight the name &lt;br /&gt;
   of your saved session and click on the load button. This will&lt;br /&gt;
   restore your saved session configuration, then click login to launch.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category : Common Tasks]]&lt;br /&gt;
[[Category : Securing FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Example_of_configuring_OpenSSH</id>
		<title>Example of configuring OpenSSH</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Example_of_configuring_OpenSSH"/>
				<updated>2012-08-13T23:18:51Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Win-client Setup Steps */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Example of configuring OpenSSH ==&lt;br /&gt;
Normally, when using native telnet, rlogin, or ftp your login ID, password and tramsmitted data is sent over the network in an clear, un-encrypted form. Network sniffers anywhere in between the client and server can steal your Login information and/or data transferred in your session. &lt;br /&gt;
&lt;br /&gt;
OpenSSH encrypts all traffic (including login ID and passwords) to effectively eliminate eavesdropping, connection hijacking, and other network-level attacks. Additionally, OpenSSH provides a myriad of secure tunneling capabilities, as well as a variety of authentication methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example Environment Description ==&lt;br /&gt;
&lt;br /&gt;
In this example we have a FreeBSD system which we will call the host. This is the system that remote users want to have login access to.&lt;br /&gt;
&lt;br /&gt;
We have an Remote FreeBSD system which is located some where on the public internet, we will call this the FBSD-client. &lt;br /&gt;
&lt;br /&gt;
We also have an Remote MS/windows system which is located some where on the public internet, we will call this the Win-client.&lt;br /&gt;
&lt;br /&gt;
This example details the encrypted Host/Client key with passphrase authentication method. This method gives the maximum protection possible utilizing ssh. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Host Setup Steps ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.  Edit /etc/rc.conf and add this statement&lt;br /&gt;
         sshd_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
     Make sure your firewall allows port 22 in from the public internet.&lt;br /&gt;
     Reboot your system to activate sshd and login as root.&lt;br /&gt;
     If this is your first time booting with sshd you will have to&lt;br /&gt;
     create the host keys.&lt;br /&gt;
     sshd will show you this on the first sshd boot only.&lt;br /&gt;
&lt;br /&gt;
    ====================================================&lt;br /&gt;
    Type a full screen full of random junk to unblock&lt;br /&gt;
    it and remember to finish with &amp;lt;enter&amp;gt;. This will&lt;br /&gt;
    timeout in 300 seconds, but waiting for&lt;br /&gt;
    the timeout without typing junk may make the&lt;br /&gt;
    entropy source deliver predictable output.&lt;br /&gt;
    Just hit &amp;lt;enter&amp;gt; for fast+insecure startup.&lt;br /&gt;
    ====================================================&lt;br /&gt;
    kern.random.sys.seeded: 1 -&amp;gt; 0&lt;br /&gt;
    qkcir83,2jsn40pl722jjbqok ---- this is the example junk entered&lt;br /&gt;
    Generating public/private rsa1 key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    ed:5d:97:dc:49:98:36:66:fc root@domainname&lt;br /&gt;
    Generating public/private dsa key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_dsa_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    67:e7:90:04:0e:27:2e:d2:97:6a root@a1poweruser.com&lt;br /&gt;
    Generating public/private rsa key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_rsa_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    96:db:50:5c:9e:69:88:26:28:54 root@domainname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2.  If you do a &amp;quot;ps ax&amp;quot; command you will see sshd as one &lt;br /&gt;
    of the running tasks.&lt;br /&gt;
&lt;br /&gt;
3.  Using adduser or pw command create a normal user account.&lt;br /&gt;
    For this example we will use bob as the host user account name.&lt;br /&gt;
&lt;br /&gt;
4.  Hit alt/F2 at same time to open second session and login using bob.&lt;br /&gt;
&lt;br /&gt;
    Run this command ssh-keygen -t rsa&lt;br /&gt;
&lt;br /&gt;
    Just hit enter to take default location and file name&lt;br /&gt;
    No need to enter a pass phrase for the host user here,&lt;br /&gt;
    just hit enter 2 times.&lt;br /&gt;
    This is what you will see&lt;br /&gt;
&lt;br /&gt;
    Generating public/private rsa key pair.&lt;br /&gt;
    Enter file in which to save the key (/bob/.ssh/id_rsa):&lt;br /&gt;
    Created directory '/bob/.ssh'.&lt;br /&gt;
    Enter passphrase (empty for no passphrase):&lt;br /&gt;
    Enter same passphrase again:&lt;br /&gt;
    Your identification has been saved in /bob/.ssh/id_rsa.&lt;br /&gt;
    Your public key has been saved in /bob/.ssh/id_rsa.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    e7:e6:8f:d3:b1:b4:08:27:09:d2 bob@domainname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
5.  If you want to use the Host 'root' account to initiate a ssh&lt;br /&gt;
    session with an remote system you have to perform step 4 above.&lt;br /&gt;
    If you want to do remote ssh login to this Host as user ‘root’, you&lt;br /&gt;
    have to edit /etc/ssh/sshd_config on the this Host and change;&lt;br /&gt;
&lt;br /&gt;
    #PermitRootLogin no  to  PermitRootLogin yes &lt;br /&gt;
&lt;br /&gt;
    Then /etc/rc.d/sshd reload  &lt;br /&gt;
&lt;br /&gt;
    to make sshd task reread it’s sshd_config file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== FBSD-client Setup Steps ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1. Using adduser or pw command create a normal user account.&lt;br /&gt;
   For this example we will use remotetom as the user account name.&lt;br /&gt;
&lt;br /&gt;
2. Login using remotetom.&lt;br /&gt;
   Run this command ssh-keygen -t rsa&lt;br /&gt;
&lt;br /&gt;
3. Just hit enter to take default location and file name.&lt;br /&gt;
   At the &amp;quot;Enter a passphrase prompt&amp;quot; [enter one and write it down, &lt;br /&gt;
   because it will be needed for ssh login to the host].&lt;br /&gt;
   This is what you will see&lt;br /&gt;
&lt;br /&gt;
   Generating public/private rsa key pair.&lt;br /&gt;
   Enter file in which to save the key (/remotetom/.ssh/id_rsa):&lt;br /&gt;
   Created directory '/remotetom/.ssh'.&lt;br /&gt;
   Enter passphrase (empty for no passphrase):&lt;br /&gt;
   Enter same passphrase again:&lt;br /&gt;
   Your identification has been saved in /remotetom/.ssh/id_rsa.&lt;br /&gt;
   Your public key has been saved in /remotetom/.ssh/id_rsa.pub.&lt;br /&gt;
   The key fingerprint is:&lt;br /&gt;
   e7:e6:8f:d3:b1:b4:08:27:09:56:de:d2 tom@domainname &lt;br /&gt;
&lt;br /&gt;
4. The Public key file you just created /remotetom/.ssh/id_rsa.pub &lt;br /&gt;
   has to be sent to the Host system. On the host system rename it&lt;br /&gt;
   to authorized_keys2 and put it into the home directory of the &lt;br /&gt;
   user setup earlier. In this case ~/bob/.ssh/authorized_keys2. &lt;br /&gt;
&lt;br /&gt;
5. To ssh to the host, enter this ssh hostname or ssh host-ip-address&lt;br /&gt;
  The first time you ssh to the Host you will get these messages.&lt;br /&gt;
  Answer yes if you are sure this first connection is with your&lt;br /&gt;
  targeted host. Enter exit to terminate session.&lt;br /&gt;
&lt;br /&gt;
   # /home/remotetom/.ssh &amp;gt;ssh 10.0.10.2&lt;br /&gt;
   The authenticity of host '10.0.10.2 (10.0.10.2)' can't be established.&lt;br /&gt;
   DSA key fingerprint is 04:0e:27:2e:d2:97:9d:7b:62:7e:c9:32:6a.&lt;br /&gt;
   Are you sure you want to continue connecting (yes/no)? yes&lt;br /&gt;
   Warning: Permanently added '10.0.10.2' (DSA) to list of known hosts.&lt;br /&gt;
   Enter passphrase for key /remotetom/.ssh/id_rsa&lt;br /&gt;
&lt;br /&gt;
   This is where you enter the pass phrase you entered while running &lt;br /&gt;
   ssh-keygen on this remote client. (remotetom)&lt;br /&gt;
&lt;br /&gt;
6. You are now logged into the Host account bob.&lt;br /&gt;
   Every time you ssh login again you will just get this prompt&lt;br /&gt;
&lt;br /&gt;
   Enter passphrase for key /remotetom/.ssh/id_rsa&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
7. At this point the Host user bob can login from Host console, or using telnet from a local LAN machine, or from the public internet if your host firewall has port 23 open. If this user is to be only allowed remote ssh client access you can use vipw command to edit the system’s master password file and star out his encrypted password. This will deny login from anywhere except remotebob&lt;br /&gt;
&lt;br /&gt;
bob:$1$FXF.CEpf:1001:31::0:0:remote ssh only #:/home/bob:/bin/csh&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
change it to look like this&lt;br /&gt;
&lt;br /&gt;
bob:*:1001:31::0:0:remote ssh only #:/home/bob:/bin/csh&lt;br /&gt;
&lt;br /&gt;
8. At this point, if remotebob is a notebook pc he can ssh login from any place in the world. If remotebob is always working from home using ip address 192.168.1.32 you can add another level of security by restricting his login to come from that ip address. Do that by adding this statement to the Host /etc/ssh/sshd_config file&lt;br /&gt;
&lt;br /&gt;
AllowUsers bob@192.168.1.32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using Midnight Commander for SFTP ==&lt;br /&gt;
&lt;br /&gt;
So far the FBSD-client setup steps above have described how to use native SSH for creating the SSH keys and logging in to the FreeBSD SSHD host. Using ssh's sftp is a command line environment. Meaning you have to know all the FTP commands to navigate around the local and remote directories plus the get and put commands to really move the files between PC's. This can really be a problem for the infrequent ftp user. An command line (I.E. not X) GUI program would make this sftp directory navigation much easier. &lt;br /&gt;
&lt;br /&gt;
We are in luck. The ports system contains 'Midnight commander' an command line GUI program designed after the MS/Windows Norton Commander. It's panel has a 2 up display containing the source and target directory trees. It's normal usage is for navigating around your File system layout. But it has built in functions which allow it to do native ftp and ssh sftp between it self and the remote ftpd or sshd host. &lt;br /&gt;
&lt;br /&gt;
The current version of MC 'IE. Midnight commander' on FreeBSD 6.0 is MC 4.6.0. There is an newer version MC 4.6.1.3 in the ports which may fix the ssh login problem described later. &lt;br /&gt;
&lt;br /&gt;
You still have to run steps 1 through 5 above to create the SSH keys and moved it to the host system and then test ssh sftp login. &lt;br /&gt;
&lt;br /&gt;
After installing MC from the ports system or package system.&lt;br /&gt;
You start it be entering  mc  on the command line.&lt;br /&gt;
This brings up the GUI panel. There are 2 ways to use MC for ftp or sftp.&lt;br /&gt;
&lt;br /&gt;
One way uses the different menu panels and allows you to save the login information as a bookmark. Use F9 to active the &lt;br /&gt;
&amp;quot;Left file command options right&amp;quot; menus at the top of the MC panel. &lt;br /&gt;
The left &amp;amp; right pull down menu option have the FTP link &amp;amp; SH link option.&lt;br /&gt;
&lt;br /&gt;
The second way is the short cut method. You just enter an &amp;quot;cd&amp;quot; change directory command on the MC command line.&lt;br /&gt;
&lt;br /&gt;
cd /#sh:user@remote Host&lt;br /&gt;
&lt;br /&gt;
user = the name of the user account on the host. In this example (bob) &lt;br /&gt;
remote host = the FQDN or IP address of the host &lt;br /&gt;
&lt;br /&gt;
The MC command line at the bottom of the panel asks for the SSH pass phrase.&lt;br /&gt;
After entering the pass phrase, the remote hosts directory tree is displayed with a heading of the /#sh: command used to launched it.&lt;br /&gt;
&lt;br /&gt;
After entering the pass phrase the MC command line does not clear and ends up containing some left over parts of the SSH message. This is a bug. &lt;br /&gt;
&lt;br /&gt;
Holding down the CTL key while pressing the keyboard letter C and O keys at the same time, 2 times in a role will clear the MC command line.&lt;br /&gt;
&lt;br /&gt;
When your finished doing SSH sftp through MC, terminate the MC SSH session with the remote host by hitting F10 key.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Win-client Setup Steps ==&lt;br /&gt;
&lt;br /&gt;
There are two MS/Windows SSH client programs. The &amp;quot;Putty&amp;quot; program which provides basic SSH access for Telnet style logins and native FTP access. The &amp;quot;WinSCP3&amp;quot; program which is built on &amp;quot;putty&amp;quot; underling code that provides a Norton commander GUI style SFTP access. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Putty Setup Steps&lt;br /&gt;
1. You have to get windows putty program from&lt;br /&gt;
   http://www.chiark.greenend.org.uk/~sgtatham/putty/&lt;br /&gt;
   Download the current windows installer program.&lt;br /&gt;
   Currently that’s putty-0.58-installer.exe&lt;br /&gt;
&lt;br /&gt;
2. Run the putty-0.58-installer.exe and take all the defaults.&lt;br /&gt;
&lt;br /&gt;
3. Start/programs/Putty/Puttygen&lt;br /&gt;
   This will create your private and public ssh keys&lt;br /&gt;
   At the bottom of the window select ssh2-rsa&lt;br /&gt;
   Click on generate button.&lt;br /&gt;
   After keys are created then enter your pass phrase.&lt;br /&gt;
   Then click on ‘save public key’ button.&lt;br /&gt;
   Save it to c:/programs/putty/putty_public_rsa_key&lt;br /&gt;
   Then click on ‘save private key’ button.&lt;br /&gt;
   Save it to c:/programs/putty/putty_private_rsa_key.ppk&lt;br /&gt;
   In the window area labeled&lt;br /&gt;
   &amp;quot;Public key for pasting into Open SSH authorized_key file&amp;quot;&lt;br /&gt;
   Highlight that content and past it into a notepad text&lt;br /&gt;
   file named win_authorized_keys2.txt&lt;br /&gt;
&lt;br /&gt;
4. The win_authorized_keys2.txt file has to be sent to the&lt;br /&gt;
   Host system and put in the ~/home/bob/.ssh/ directory.&lt;br /&gt;
   In this example both the FBSD-client &amp;amp; the WIN-client will&lt;br /&gt;
   both use the same Host user account to ssh login to.&lt;br /&gt;
   Since the ~/home/bob/.ssh/authorized_keys2 already contains the&lt;br /&gt;
   private key for the FBSD-client you have to append the&lt;br /&gt;
   win_authorized_keys2.txt content to it.&lt;br /&gt;
&lt;br /&gt;
   cat win_authorized_keys2.txt &amp;gt;&amp;gt; authorized_keys2&lt;br /&gt;
&lt;br /&gt;
   If bob is only to be used by WIN-client then just&lt;br /&gt;
&lt;br /&gt;
   cp win_authorized_keys2.txt authorized_keys2&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
5. start/programs/putty/putty to run it&lt;br /&gt;
   On the bottom of the window click on &amp;quot;never exit&amp;quot;&lt;br /&gt;
   Under protocol be sure ssh is clicked.&lt;br /&gt;
   On the left side click on ‘SSH’, then in the&lt;br /&gt;
   ‘preferred ssh protocol version’ click on ‘2 only’.&lt;br /&gt;
   On the left side click on ‘AUTH’, remove check mark from&lt;br /&gt;
   ‘Attempt keyboard-interactive auth (SSH-2).&lt;br /&gt;
   On the left side click on ‘Sessions’ to return to main login window.&lt;br /&gt;
   Enter you host name (IE; your official registered domain name of&lt;br /&gt;
   the Host box) or it’s IP address. Then type a name in the saved&lt;br /&gt;
   session window and click on save button. Now click on the open button&lt;br /&gt;
   at bottom of window to login to the host.&lt;br /&gt;
&lt;br /&gt;
   A ms/dos window will open up and you will receive ‘login as’ prompt &lt;br /&gt;
   Enter bob&lt;br /&gt;
   Then you get&lt;br /&gt;
   Authenticating with public key &amp;quot;rsa-key-20060209&amp;quot;&lt;br /&gt;
   Passphrase for key &amp;quot;rsa-key-20060209&amp;quot;:&lt;br /&gt;
   At this point enter the pass phrase you entered when you ran &lt;br /&gt;
   Start/programs/Putty/Puttygen to create the rsa keys on&lt;br /&gt;
   the client remotetom.&lt;br /&gt;
&lt;br /&gt;
6. You are now logged into you host box using SSH. You have all &lt;br /&gt;
   the privileges that host user bob has when you created him.&lt;br /&gt;
   Every time you start/programs/putty/putty highlight the name&lt;br /&gt;
   of your saved session and click on the load button. This will &lt;br /&gt;
   restore your saved session configuration, then click open to launch.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== WinSCP3 Setup Steps ==&lt;br /&gt;
 &lt;br /&gt;
1. You have to get windows WinSCP3 program from&lt;br /&gt;
   http://winscp.net/eng/index.php&lt;br /&gt;
   Download the windows installer program.&lt;br /&gt;
   Currently that's winscp380setup.exe&lt;br /&gt;
&lt;br /&gt;
2. Run winscp380setup.exe and take all the defaults.&lt;br /&gt;
&lt;br /&gt;
3. Start/programs/WinSCP3/keytools/puttygen&lt;br /&gt;
   This will create your private and public ssh keys &lt;br /&gt;
&lt;br /&gt;
   At the bottom of the window select ssh2-rsa&lt;br /&gt;
   Click on generate button.&lt;br /&gt;
&lt;br /&gt;
   After keys are created then enter your pass phrase.&lt;br /&gt;
&lt;br /&gt;
   Then click on 'save public key' button.&lt;br /&gt;
   Save it to c:/Program Files/WinSCP3/putty_public_rsa_key&lt;br /&gt;
&lt;br /&gt;
   Then click on 'save private key' button.&lt;br /&gt;
   Save it to c:/Program Files/WinSCP3/putty_private_rsa_key.ppk&lt;br /&gt;
&lt;br /&gt;
   In the window area labeled &lt;br /&gt;
   &amp;quot;Public key for pasting into Open SSH authorized_key file&amp;quot; &lt;br /&gt;
   Highlight that content and past it into a notepad text file&lt;br /&gt;
   named win_authorized_keys2.txt&lt;br /&gt;
&lt;br /&gt;
4. The win_authorized_keys2.txt file has to be sent to the Host system &lt;br /&gt;
   and put in the ~/home/bob/.ssh/ directory. In this example both &lt;br /&gt;
   the FBSD-client &amp;amp; the WIN-client will both use the same Host user &lt;br /&gt;
   account to ssh login to. &lt;br /&gt;
&lt;br /&gt;
   Since the ~/home/bob/.ssh/authorized_keys2 already contains the&lt;br /&gt;
   private key for the FBSD-client you have to append the&lt;br /&gt;
   win_authorized_keys2.txt content to it.&lt;br /&gt;
&lt;br /&gt;
    cat win_authorized_keys2.txt &amp;gt;&amp;gt; authorized_keys2&lt;br /&gt;
&lt;br /&gt;
   If bob is to only be used by WIN-client then just &lt;br /&gt;
      cp win_authorized_keys2.txt authorized_keys2&lt;br /&gt;
&lt;br /&gt;
5. Start/programs/WinSCP3/WinSCP3&lt;br /&gt;
   In the window on the left, click on SSH&lt;br /&gt;
   Then for preferred ssh protocol version, click on &amp;quot;2 only&amp;quot;&lt;br /&gt;
&lt;br /&gt;
   In the window on the left, click on session&lt;br /&gt;
   Then fill in your host name FQDN or IP address.&lt;br /&gt;
   Then the user name of the host account you want to login to.&lt;br /&gt;
   Then fill in the location of for the rsa putty key created&lt;br /&gt;
   in step 3 above&lt;br /&gt;
   c:/Program Files/WinSCP3/putty_private_rsa_key.ppk&lt;br /&gt;
&lt;br /&gt;
   For protocol click on sftp &lt;br /&gt;
&lt;br /&gt;
   At the bottom of the panel click on the save button&lt;br /&gt;
   Accept the name in the pop up window or enter your own&lt;br /&gt;
   value and click OK.&lt;br /&gt;
&lt;br /&gt;
   The stored session panel opens, highlight the session name you want &lt;br /&gt;
   to run and click on the load button. This fills in the session panel&lt;br /&gt;
   with your saved configuration.&lt;br /&gt;
&lt;br /&gt;
   Click on login button to initiate your sftp session with the host.&lt;br /&gt;
   A pop up window will show asking you to enter the pass phrase you &lt;br /&gt;
   entered in &amp;quot;Step 3&amp;quot; above. Click on OK after entering you pass phrase.&lt;br /&gt;
&lt;br /&gt;
   The first time you SSH to the Host you will get these messages. &lt;br /&gt;
   Answer yes if you are sure this first connection is with your&lt;br /&gt;
   targeted host.&lt;br /&gt;
&lt;br /&gt;
   The authenticity of host '10.0.10.2 (10.0.10.2)' can't be established.&lt;br /&gt;
   DSA key fingerprint is 67:e7:90:04:0e:27:2e:d2.&lt;br /&gt;
   Are you sure you want to continue connecting (yes/no)? yes&lt;br /&gt;
   Warning: Permanently added 10.0.10.2 (DSA) to the list of known hosts.&lt;br /&gt;
&lt;br /&gt;
   The GUI Norton commander style panel will display.&lt;br /&gt;
   Left side shows the client's directory and the &lt;br /&gt;
   right side shows the Host user accounts directory &lt;br /&gt;
&lt;br /&gt;
   Every time you start/programs/WinSCP3/WinSCP3 highlight the name &lt;br /&gt;
   of your saved session and click on the load button. This will&lt;br /&gt;
   restore your saved session configuration, then click login to launch.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category : Common Tasks]]&lt;br /&gt;
[[Category : Securing FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Example_of_configuring_OpenSSH</id>
		<title>Example of configuring OpenSSH</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Example_of_configuring_OpenSSH"/>
				<updated>2012-08-13T23:16:43Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* FBSD-client Setup Steps */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Example of configuring OpenSSH ==&lt;br /&gt;
Normally, when using native telnet, rlogin, or ftp your login ID, password and tramsmitted data is sent over the network in an clear, un-encrypted form. Network sniffers anywhere in between the client and server can steal your Login information and/or data transferred in your session. &lt;br /&gt;
&lt;br /&gt;
OpenSSH encrypts all traffic (including login ID and passwords) to effectively eliminate eavesdropping, connection hijacking, and other network-level attacks. Additionally, OpenSSH provides a myriad of secure tunneling capabilities, as well as a variety of authentication methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example Environment Description ==&lt;br /&gt;
&lt;br /&gt;
In this example we have a FreeBSD system which we will call the host. This is the system that remote users want to have login access to.&lt;br /&gt;
&lt;br /&gt;
We have an Remote FreeBSD system which is located some where on the public internet, we will call this the FBSD-client. &lt;br /&gt;
&lt;br /&gt;
We also have an Remote MS/windows system which is located some where on the public internet, we will call this the Win-client.&lt;br /&gt;
&lt;br /&gt;
This example details the encrypted Host/Client key with passphrase authentication method. This method gives the maximum protection possible utilizing ssh. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Host Setup Steps ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.  Edit /etc/rc.conf and add this statement&lt;br /&gt;
         sshd_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
     Make sure your firewall allows port 22 in from the public internet.&lt;br /&gt;
     Reboot your system to activate sshd and login as root.&lt;br /&gt;
     If this is your first time booting with sshd you will have to&lt;br /&gt;
     create the host keys.&lt;br /&gt;
     sshd will show you this on the first sshd boot only.&lt;br /&gt;
&lt;br /&gt;
    ====================================================&lt;br /&gt;
    Type a full screen full of random junk to unblock&lt;br /&gt;
    it and remember to finish with &amp;lt;enter&amp;gt;. This will&lt;br /&gt;
    timeout in 300 seconds, but waiting for&lt;br /&gt;
    the timeout without typing junk may make the&lt;br /&gt;
    entropy source deliver predictable output.&lt;br /&gt;
    Just hit &amp;lt;enter&amp;gt; for fast+insecure startup.&lt;br /&gt;
    ====================================================&lt;br /&gt;
    kern.random.sys.seeded: 1 -&amp;gt; 0&lt;br /&gt;
    qkcir83,2jsn40pl722jjbqok ---- this is the example junk entered&lt;br /&gt;
    Generating public/private rsa1 key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    ed:5d:97:dc:49:98:36:66:fc root@domainname&lt;br /&gt;
    Generating public/private dsa key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_dsa_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    67:e7:90:04:0e:27:2e:d2:97:6a root@a1poweruser.com&lt;br /&gt;
    Generating public/private rsa key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_rsa_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    96:db:50:5c:9e:69:88:26:28:54 root@domainname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2.  If you do a &amp;quot;ps ax&amp;quot; command you will see sshd as one &lt;br /&gt;
    of the running tasks.&lt;br /&gt;
&lt;br /&gt;
3.  Using adduser or pw command create a normal user account.&lt;br /&gt;
    For this example we will use bob as the host user account name.&lt;br /&gt;
&lt;br /&gt;
4.  Hit alt/F2 at same time to open second session and login using bob.&lt;br /&gt;
&lt;br /&gt;
    Run this command ssh-keygen -t rsa&lt;br /&gt;
&lt;br /&gt;
    Just hit enter to take default location and file name&lt;br /&gt;
    No need to enter a pass phrase for the host user here,&lt;br /&gt;
    just hit enter 2 times.&lt;br /&gt;
    This is what you will see&lt;br /&gt;
&lt;br /&gt;
    Generating public/private rsa key pair.&lt;br /&gt;
    Enter file in which to save the key (/bob/.ssh/id_rsa):&lt;br /&gt;
    Created directory '/bob/.ssh'.&lt;br /&gt;
    Enter passphrase (empty for no passphrase):&lt;br /&gt;
    Enter same passphrase again:&lt;br /&gt;
    Your identification has been saved in /bob/.ssh/id_rsa.&lt;br /&gt;
    Your public key has been saved in /bob/.ssh/id_rsa.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    e7:e6:8f:d3:b1:b4:08:27:09:d2 bob@domainname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
5.  If you want to use the Host 'root' account to initiate a ssh&lt;br /&gt;
    session with an remote system you have to perform step 4 above.&lt;br /&gt;
    If you want to do remote ssh login to this Host as user ‘root’, you&lt;br /&gt;
    have to edit /etc/ssh/sshd_config on the this Host and change;&lt;br /&gt;
&lt;br /&gt;
    #PermitRootLogin no  to  PermitRootLogin yes &lt;br /&gt;
&lt;br /&gt;
    Then /etc/rc.d/sshd reload  &lt;br /&gt;
&lt;br /&gt;
    to make sshd task reread it’s sshd_config file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== FBSD-client Setup Steps ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1. Using adduser or pw command create a normal user account.&lt;br /&gt;
   For this example we will use remotetom as the user account name.&lt;br /&gt;
&lt;br /&gt;
2. Login using remotetom.&lt;br /&gt;
   Run this command ssh-keygen -t rsa&lt;br /&gt;
&lt;br /&gt;
3. Just hit enter to take default location and file name.&lt;br /&gt;
   At the &amp;quot;Enter a passphrase prompt&amp;quot; [enter one and write it down, &lt;br /&gt;
   because it will be needed for ssh login to the host].&lt;br /&gt;
   This is what you will see&lt;br /&gt;
&lt;br /&gt;
   Generating public/private rsa key pair.&lt;br /&gt;
   Enter file in which to save the key (/remotetom/.ssh/id_rsa):&lt;br /&gt;
   Created directory '/remotetom/.ssh'.&lt;br /&gt;
   Enter passphrase (empty for no passphrase):&lt;br /&gt;
   Enter same passphrase again:&lt;br /&gt;
   Your identification has been saved in /remotetom/.ssh/id_rsa.&lt;br /&gt;
   Your public key has been saved in /remotetom/.ssh/id_rsa.pub.&lt;br /&gt;
   The key fingerprint is:&lt;br /&gt;
   e7:e6:8f:d3:b1:b4:08:27:09:56:de:d2 tom@domainname &lt;br /&gt;
&lt;br /&gt;
4. The Public key file you just created /remotetom/.ssh/id_rsa.pub &lt;br /&gt;
   has to be sent to the Host system. On the host system rename it&lt;br /&gt;
   to authorized_keys2 and put it into the home directory of the &lt;br /&gt;
   user setup earlier. In this case ~/bob/.ssh/authorized_keys2. &lt;br /&gt;
&lt;br /&gt;
5. To ssh to the host, enter this ssh hostname or ssh host-ip-address&lt;br /&gt;
  The first time you ssh to the Host you will get these messages.&lt;br /&gt;
  Answer yes if you are sure this first connection is with your&lt;br /&gt;
  targeted host. Enter exit to terminate session.&lt;br /&gt;
&lt;br /&gt;
   # /home/remotetom/.ssh &amp;gt;ssh 10.0.10.2&lt;br /&gt;
   The authenticity of host '10.0.10.2 (10.0.10.2)' can't be established.&lt;br /&gt;
   DSA key fingerprint is 04:0e:27:2e:d2:97:9d:7b:62:7e:c9:32:6a.&lt;br /&gt;
   Are you sure you want to continue connecting (yes/no)? yes&lt;br /&gt;
   Warning: Permanently added '10.0.10.2' (DSA) to list of known hosts.&lt;br /&gt;
   Enter passphrase for key /remotetom/.ssh/id_rsa&lt;br /&gt;
&lt;br /&gt;
   This is where you enter the pass phrase you entered while running &lt;br /&gt;
   ssh-keygen on this remote client. (remotetom)&lt;br /&gt;
&lt;br /&gt;
6. You are now logged into the Host account bob.&lt;br /&gt;
   Every time you ssh login again you will just get this prompt&lt;br /&gt;
&lt;br /&gt;
   Enter passphrase for key /remotetom/.ssh/id_rsa&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
7. At this point the Host user bob can login from Host console, or using telnet from a local LAN machine, or from the public internet if your host firewall has port 23 open. If this user is to be only allowed remote ssh client access you can use vipw command to edit the system’s master password file and star out his encrypted password. This will deny login from anywhere except remotebob&lt;br /&gt;
&lt;br /&gt;
bob:$1$FXF.CEpf:1001:31::0:0:remote ssh only #:/home/bob:/bin/csh&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
change it to look like this&lt;br /&gt;
&lt;br /&gt;
bob:*:1001:31::0:0:remote ssh only #:/home/bob:/bin/csh&lt;br /&gt;
&lt;br /&gt;
8. At this point, if remotebob is a notebook pc he can ssh login from any place in the world. If remotebob is always working from home using ip address 192.168.1.32 you can add another level of security by restricting his login to come from that ip address. Do that by adding this statement to the Host /etc/ssh/sshd_config file&lt;br /&gt;
&lt;br /&gt;
AllowUsers bob@192.168.1.32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using Midnight Commander for SFTP ==&lt;br /&gt;
&lt;br /&gt;
So far the FBSD-client setup steps above have described how to use native SSH for creating the SSH keys and logging in to the FreeBSD SSHD host. Using ssh's sftp is a command line environment. Meaning you have to know all the FTP commands to navigate around the local and remote directories plus the get and put commands to really move the files between PC's. This can really be a problem for the infrequent ftp user. An command line (I.E. not X) GUI program would make this sftp directory navigation much easier. &lt;br /&gt;
&lt;br /&gt;
We are in luck. The ports system contains 'Midnight commander' an command line GUI program designed after the MS/Windows Norton Commander. It's panel has a 2 up display containing the source and target directory trees. It's normal usage is for navigating around your File system layout. But it has built in functions which allow it to do native ftp and ssh sftp between it self and the remote ftpd or sshd host. &lt;br /&gt;
&lt;br /&gt;
The current version of MC 'IE. Midnight commander' on FreeBSD 6.0 is MC 4.6.0. There is an newer version MC 4.6.1.3 in the ports which may fix the ssh login problem described later. &lt;br /&gt;
&lt;br /&gt;
You still have to run steps 1 through 5 above to create the SSH keys and moved it to the host system and then test ssh sftp login. &lt;br /&gt;
&lt;br /&gt;
After installing MC from the ports system or package system.&lt;br /&gt;
You start it be entering  mc  on the command line.&lt;br /&gt;
This brings up the GUI panel. There are 2 ways to use MC for ftp or sftp.&lt;br /&gt;
&lt;br /&gt;
One way uses the different menu panels and allows you to save the login information as a bookmark. Use F9 to active the &lt;br /&gt;
&amp;quot;Left file command options right&amp;quot; menus at the top of the MC panel. &lt;br /&gt;
The left &amp;amp; right pull down menu option have the FTP link &amp;amp; SH link option.&lt;br /&gt;
&lt;br /&gt;
The second way is the short cut method. You just enter an &amp;quot;cd&amp;quot; change directory command on the MC command line.&lt;br /&gt;
&lt;br /&gt;
cd /#sh:user@remote Host&lt;br /&gt;
&lt;br /&gt;
user = the name of the user account on the host. In this example (bob) &lt;br /&gt;
remote host = the FQDN or IP address of the host &lt;br /&gt;
&lt;br /&gt;
The MC command line at the bottom of the panel asks for the SSH pass phrase.&lt;br /&gt;
After entering the pass phrase, the remote hosts directory tree is displayed with a heading of the /#sh: command used to launched it.&lt;br /&gt;
&lt;br /&gt;
After entering the pass phrase the MC command line does not clear and ends up containing some left over parts of the SSH message. This is a bug. &lt;br /&gt;
&lt;br /&gt;
Holding down the CTL key while pressing the keyboard letter C and O keys at the same time, 2 times in a role will clear the MC command line.&lt;br /&gt;
&lt;br /&gt;
When your finished doing SSH sftp through MC, terminate the MC SSH session with the remote host by hitting F10 key.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Win-client Setup Steps ==&lt;br /&gt;
&lt;br /&gt;
There are two MS/Windows SSH client programs. The &amp;quot;Putty&amp;quot; program which provides basic SSH access for Telnet style logins and native FTP access. The &amp;quot;WinSCP3&amp;quot; program which is built on &amp;quot;putty&amp;quot; underling code that provides a Norton commander GUI style SFTP access. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Putty Setup Steps&lt;br /&gt;
1. You have to get windows putty program from&lt;br /&gt;
   http://www.chiark.greenend.org.uk/~sgtatham/putty/&lt;br /&gt;
   Download the current windows installer program.&lt;br /&gt;
   Currently that’s putty-0.58-installer.exe&lt;br /&gt;
&lt;br /&gt;
2. Run the putty-0.58-installer.exe and take all the defaults.&lt;br /&gt;
&lt;br /&gt;
3. Start/programs/Putty/Puttygen&lt;br /&gt;
   This will create your private and public ssh keys&lt;br /&gt;
   At the bottom of the window select ssh2-rsa&lt;br /&gt;
   Click on generate button.&lt;br /&gt;
   After keys are created then enter your pass phrase.&lt;br /&gt;
   Then click on ‘save public key’ button.&lt;br /&gt;
   Save it to c:/programs/putty/putty_public_rsa_key&lt;br /&gt;
   Then click on ‘save private key’ button.&lt;br /&gt;
   Save it to c:/programs/putty/putty_private_rsa_key.ppk&lt;br /&gt;
   In the window area labeled&lt;br /&gt;
   &amp;quot;Public key for pasting into Open SSH authorized_key file&amp;quot;&lt;br /&gt;
   Highlight that content and past it into a notepad text&lt;br /&gt;
   file named win_authorized_keys2.txt&lt;br /&gt;
&lt;br /&gt;
4. The win_authorized_keys2.txt file has to be sent to the&lt;br /&gt;
   Host system and put in the ~/home/bob/.ssh/ directory.&lt;br /&gt;
   In this example both the FBSD-client &amp;amp; the WIN-client will&lt;br /&gt;
   both use the same Host user account to ssh login to.&lt;br /&gt;
   Since the ~/home/bob/.ssh/authorized_keys2 already contains the&lt;br /&gt;
   private key for the FBSD-client you have to append the&lt;br /&gt;
   win_authorized_keys2.txt content to it.&lt;br /&gt;
&lt;br /&gt;
   cat win_authorized_keys2.txt &amp;gt;&amp;gt; authorized_keys2&lt;br /&gt;
&lt;br /&gt;
   If bob is only to be used by WIN-client then just&lt;br /&gt;
&lt;br /&gt;
   cp win_authorized_keys2.txt authorized_keys2&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
5. start/programs/putty/putty to run it&lt;br /&gt;
   On the bottom of the window click on &amp;quot;never exit&amp;quot;&lt;br /&gt;
   Under protocol be sure ssh is clicked.&lt;br /&gt;
   On the left side click on ‘SSH’, then in the&lt;br /&gt;
   ‘preferred ssh protocol version’ click on ‘2 only’.&lt;br /&gt;
   On the left side click on ‘AUTH’, remove check mark from&lt;br /&gt;
   ‘Attempt keyboard-interactive auth (SSH-2).&lt;br /&gt;
   On the left side click on ‘Sessions’ to return to main login window.&lt;br /&gt;
   Enter you host name (IE; your official registered domain name of&lt;br /&gt;
   the Host box) or it’s IP address. Then type a name in the saved&lt;br /&gt;
   session window and click on save button. Now click on the open button&lt;br /&gt;
   at bottom of window to login to the host.&lt;br /&gt;
&lt;br /&gt;
   A ms/dos window will open up and you will receive ‘login as’ prompt &lt;br /&gt;
   Enter bob&lt;br /&gt;
   Then you get&lt;br /&gt;
   Authenticating with public key &amp;quot;rsa-key-20060209&amp;quot;&lt;br /&gt;
   Passphrase for key &amp;quot;rsa-key-20060209&amp;quot;:&lt;br /&gt;
   At this point enter the pass phrase you entered when you ran &lt;br /&gt;
   Start/programs/Putty/Puttygen to create the rsa keys on&lt;br /&gt;
   the client remotetom.&lt;br /&gt;
&lt;br /&gt;
6. You are now logged into you host box using SSH. You have all &lt;br /&gt;
   the privileges that host user bob has when you created him.&lt;br /&gt;
   Every time you start/programs/putty/putty highlight the name&lt;br /&gt;
   of your saved session and click on the load button. This will &lt;br /&gt;
   restore your saved session configuration, then click open to launch.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== WinSCP3 Setup Steps ==&lt;br /&gt;
 &lt;br /&gt;
1. You have to get windows WinSCP3 program from&lt;br /&gt;
   http://winscp.net/eng/index.php&lt;br /&gt;
   Download the windows installer program.&lt;br /&gt;
   Currently that's winscp380setup.exe&lt;br /&gt;
&lt;br /&gt;
2. Run winscp380setup.exe and take all the defaults.&lt;br /&gt;
&lt;br /&gt;
3. Start/programs/WinSCP3/keytools/puttygen&lt;br /&gt;
   This will create your private and public ssh keys &lt;br /&gt;
&lt;br /&gt;
   At the bottom of the window select ssh2-rsa&lt;br /&gt;
   Click on generate button.&lt;br /&gt;
&lt;br /&gt;
   After keys are created then enter your pass phrase.&lt;br /&gt;
&lt;br /&gt;
   Then click on 'save public key' button.&lt;br /&gt;
   Save it to c:/Program Files/WinSCP3/putty_public_rsa_key&lt;br /&gt;
&lt;br /&gt;
   Then click on 'save private key' button.&lt;br /&gt;
   Save it to c:/Program Files/WinSCP3/putty_private_rsa_key.ppk&lt;br /&gt;
&lt;br /&gt;
   In the window area labeled &lt;br /&gt;
   &amp;quot;Public key for pasting into Open SSH authorized_key file&amp;quot; &lt;br /&gt;
   Highlight that content and past it into a notepad text file&lt;br /&gt;
   named win_authorized_keys2.txt&lt;br /&gt;
&lt;br /&gt;
4. The win_authorized_keys2.txt file has to be sent to the Host system &lt;br /&gt;
   and put in the ~/home/bob/.ssh/ directory. In this example both &lt;br /&gt;
   the FBSD-client &amp;amp; the WIN-client will both use the same Host user &lt;br /&gt;
   account to ssh login to. &lt;br /&gt;
&lt;br /&gt;
   Since the ~/home/bob/.ssh/authorized_keys2 already contains the&lt;br /&gt;
   private key for the FBSD-client you have to append the&lt;br /&gt;
   win_authorized_keys2.txt content to it.&lt;br /&gt;
&lt;br /&gt;
    cat win_authorized_keys2.txt &amp;gt;&amp;gt; authorized_keys2&lt;br /&gt;
&lt;br /&gt;
   If bob is to only be used by WIN-client then just &lt;br /&gt;
      cp win_authorized_keys2.txt authorized_keys2&lt;br /&gt;
&lt;br /&gt;
5. Start/programs/WinSCP3/WinSCP3&lt;br /&gt;
   In the window on the left, click on SSH&lt;br /&gt;
   Then for preferred ssh protocol version, click on &amp;quot;2 only&amp;quot;&lt;br /&gt;
&lt;br /&gt;
   In the window on the left, click on session&lt;br /&gt;
   Then fill in your host name FQDN or IP address.&lt;br /&gt;
   Then the user name of the host account you want to login to.&lt;br /&gt;
   Then fill in the location of for the rsa putty key created&lt;br /&gt;
   in step 3 above&lt;br /&gt;
   c:/Program Files/WinSCP3/putty_private_rsa_key.ppk&lt;br /&gt;
&lt;br /&gt;
   For protocol click on sftp &lt;br /&gt;
&lt;br /&gt;
   At the bottom of the panel click on the save button&lt;br /&gt;
   Accept the name in the pop up window or enter your own&lt;br /&gt;
   value and click OK.&lt;br /&gt;
&lt;br /&gt;
   The stored session panel opens, highlight the session name you want &lt;br /&gt;
   to run and click on the load button. This fills in the session panel&lt;br /&gt;
   with your saved configuration.&lt;br /&gt;
&lt;br /&gt;
   Click on login button to initiate your sftp session with the host.&lt;br /&gt;
   A pop up window will show asking you to enter the pass phrase you &lt;br /&gt;
   entered in &amp;quot;Step 3&amp;quot; above. Click on OK after entering you pass phrase.&lt;br /&gt;
&lt;br /&gt;
   The first time you SSH to the Host you will get these messages. &lt;br /&gt;
   Answer yes if you are sure this first connection is with your&lt;br /&gt;
   targeted host.&lt;br /&gt;
&lt;br /&gt;
   The authenticity of host '10.0.10.2 (10.0.10.2)' can't be established.&lt;br /&gt;
   DSA key fingerprint is 67:e7:90:04:0e:27:2e:d2.&lt;br /&gt;
   Are you sure you want to continue connecting (yes/no)? yes&lt;br /&gt;
   Warning: Permanently added 10.0.10.2 (DSA) to the list of known hosts.&lt;br /&gt;
&lt;br /&gt;
   The GUI Norton commander style panel will display.&lt;br /&gt;
   Left side shows the client's directory and the &lt;br /&gt;
   right side shows the Host user accounts directory &lt;br /&gt;
&lt;br /&gt;
   Every time you start/programs/WinSCP3/WinSCP3 highlight the name &lt;br /&gt;
   of your saved session and click on the load button. This will&lt;br /&gt;
   restore your saved session configuration, then click login to launch.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category : Common Tasks]]&lt;br /&gt;
[[Category : Securing FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Example_of_configuring_OpenSSH</id>
		<title>Example of configuring OpenSSH</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Example_of_configuring_OpenSSH"/>
				<updated>2012-08-13T23:14:53Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Host Setup Steps */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Example of configuring OpenSSH ==&lt;br /&gt;
Normally, when using native telnet, rlogin, or ftp your login ID, password and tramsmitted data is sent over the network in an clear, un-encrypted form. Network sniffers anywhere in between the client and server can steal your Login information and/or data transferred in your session. &lt;br /&gt;
&lt;br /&gt;
OpenSSH encrypts all traffic (including login ID and passwords) to effectively eliminate eavesdropping, connection hijacking, and other network-level attacks. Additionally, OpenSSH provides a myriad of secure tunneling capabilities, as well as a variety of authentication methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example Environment Description ==&lt;br /&gt;
&lt;br /&gt;
In this example we have a FreeBSD system which we will call the host. This is the system that remote users want to have login access to.&lt;br /&gt;
&lt;br /&gt;
We have an Remote FreeBSD system which is located some where on the public internet, we will call this the FBSD-client. &lt;br /&gt;
&lt;br /&gt;
We also have an Remote MS/windows system which is located some where on the public internet, we will call this the Win-client.&lt;br /&gt;
&lt;br /&gt;
This example details the encrypted Host/Client key with passphrase authentication method. This method gives the maximum protection possible utilizing ssh. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Host Setup Steps ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.  Edit /etc/rc.conf and add this statement&lt;br /&gt;
         sshd_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
     Make sure your firewall allows port 22 in from the public internet.&lt;br /&gt;
     Reboot your system to activate sshd and login as root.&lt;br /&gt;
     If this is your first time booting with sshd you will have to&lt;br /&gt;
     create the host keys.&lt;br /&gt;
     sshd will show you this on the first sshd boot only.&lt;br /&gt;
&lt;br /&gt;
    ====================================================&lt;br /&gt;
    Type a full screen full of random junk to unblock&lt;br /&gt;
    it and remember to finish with &amp;lt;enter&amp;gt;. This will&lt;br /&gt;
    timeout in 300 seconds, but waiting for&lt;br /&gt;
    the timeout without typing junk may make the&lt;br /&gt;
    entropy source deliver predictable output.&lt;br /&gt;
    Just hit &amp;lt;enter&amp;gt; for fast+insecure startup.&lt;br /&gt;
    ====================================================&lt;br /&gt;
    kern.random.sys.seeded: 1 -&amp;gt; 0&lt;br /&gt;
    qkcir83,2jsn40pl722jjbqok ---- this is the example junk entered&lt;br /&gt;
    Generating public/private rsa1 key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    ed:5d:97:dc:49:98:36:66:fc root@domainname&lt;br /&gt;
    Generating public/private dsa key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_dsa_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    67:e7:90:04:0e:27:2e:d2:97:6a root@a1poweruser.com&lt;br /&gt;
    Generating public/private rsa key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_rsa_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    96:db:50:5c:9e:69:88:26:28:54 root@domainname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2.  If you do a &amp;quot;ps ax&amp;quot; command you will see sshd as one &lt;br /&gt;
    of the running tasks.&lt;br /&gt;
&lt;br /&gt;
3.  Using adduser or pw command create a normal user account.&lt;br /&gt;
    For this example we will use bob as the host user account name.&lt;br /&gt;
&lt;br /&gt;
4.  Hit alt/F2 at same time to open second session and login using bob.&lt;br /&gt;
&lt;br /&gt;
    Run this command ssh-keygen -t rsa&lt;br /&gt;
&lt;br /&gt;
    Just hit enter to take default location and file name&lt;br /&gt;
    No need to enter a pass phrase for the host user here,&lt;br /&gt;
    just hit enter 2 times.&lt;br /&gt;
    This is what you will see&lt;br /&gt;
&lt;br /&gt;
    Generating public/private rsa key pair.&lt;br /&gt;
    Enter file in which to save the key (/bob/.ssh/id_rsa):&lt;br /&gt;
    Created directory '/bob/.ssh'.&lt;br /&gt;
    Enter passphrase (empty for no passphrase):&lt;br /&gt;
    Enter same passphrase again:&lt;br /&gt;
    Your identification has been saved in /bob/.ssh/id_rsa.&lt;br /&gt;
    Your public key has been saved in /bob/.ssh/id_rsa.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    e7:e6:8f:d3:b1:b4:08:27:09:d2 bob@domainname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
5.  If you want to use the Host 'root' account to initiate a ssh&lt;br /&gt;
    session with an remote system you have to perform step 4 above.&lt;br /&gt;
    If you want to do remote ssh login to this Host as user ‘root’, you&lt;br /&gt;
    have to edit /etc/ssh/sshd_config on the this Host and change;&lt;br /&gt;
&lt;br /&gt;
    #PermitRootLogin no  to  PermitRootLogin yes &lt;br /&gt;
&lt;br /&gt;
    Then /etc/rc.d/sshd reload  &lt;br /&gt;
&lt;br /&gt;
    to make sshd task reread it’s sshd_config file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== FBSD-client Setup Steps ==&lt;br /&gt;
&lt;br /&gt;
1. Using adduser or pw command create a normal user account.&lt;br /&gt;
   For this example we will use remotetom as the user account name.&lt;br /&gt;
&lt;br /&gt;
2. Login using remotetom.&lt;br /&gt;
   Run this command ssh-keygen -t rsa&lt;br /&gt;
&lt;br /&gt;
3. Just hit enter to take default location and file name.&lt;br /&gt;
   At the &amp;quot;Enter a passphrase prompt&amp;quot; [enter one and write it down, &lt;br /&gt;
   because it will be needed for ssh login to the host].&lt;br /&gt;
   This is what you will see&lt;br /&gt;
&lt;br /&gt;
   Generating public/private rsa key pair.&lt;br /&gt;
   Enter file in which to save the key (/remotetom/.ssh/id_rsa):&lt;br /&gt;
   Created directory '/remotetom/.ssh'.&lt;br /&gt;
   Enter passphrase (empty for no passphrase):&lt;br /&gt;
   Enter same passphrase again:&lt;br /&gt;
   Your identification has been saved in /remotetom/.ssh/id_rsa.&lt;br /&gt;
   Your public key has been saved in /remotetom/.ssh/id_rsa.pub.&lt;br /&gt;
   The key fingerprint is:&lt;br /&gt;
   e7:e6:8f:d3:b1:b4:08:27:09:56:de:d2 tom@domainname &lt;br /&gt;
&lt;br /&gt;
4. The Public key file you just created /remotetom/.ssh/id_rsa.pub &lt;br /&gt;
   has to be sent to the Host system. On the host system rename it&lt;br /&gt;
   to authorized_keys2 and put it into the home directory of the &lt;br /&gt;
   user setup earlier. In this case ~/bob/.ssh/authorized_keys2. &lt;br /&gt;
&lt;br /&gt;
5. To ssh to the host, enter this ssh hostname or ssh host-ip-address&lt;br /&gt;
  The first time you ssh to the Host you will get these messages.&lt;br /&gt;
  Answer yes if you are sure this first connection is with your&lt;br /&gt;
  targeted host. Enter exit to terminate session.&lt;br /&gt;
&lt;br /&gt;
   # /home/remotetom/.ssh &amp;gt;ssh 10.0.10.2&lt;br /&gt;
   The authenticity of host '10.0.10.2 (10.0.10.2)' can't be established.&lt;br /&gt;
   DSA key fingerprint is 04:0e:27:2e:d2:97:9d:7b:62:7e:c9:32:6a.&lt;br /&gt;
   Are you sure you want to continue connecting (yes/no)? yes&lt;br /&gt;
   Warning: Permanently added '10.0.10.2' (DSA) to list of known hosts.&lt;br /&gt;
   Enter passphrase for key /remotetom/.ssh/id_rsa&lt;br /&gt;
&lt;br /&gt;
   This is where you enter the pass phrase you entered while running &lt;br /&gt;
   ssh-keygen on this remote client. (remotetom)&lt;br /&gt;
&lt;br /&gt;
6. You are now logged into the Host account bob.&lt;br /&gt;
   Every time you ssh login again you will just get this prompt&lt;br /&gt;
&lt;br /&gt;
   Enter passphrase for key /remotetom/.ssh/id_rsa&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
7. At this point the Host user bob can login from Host console, or using telnet from a local LAN machine, or from the public internet if your host firewall has port 23 open. If this user is to be only allowed remote ssh client access you can use vipw command to edit the system’s master password file and star out his encrypted password. This will deny login from anywhere except remotebob&lt;br /&gt;
&lt;br /&gt;
bob:$1$FXF.CEpf:1001:31::0:0:remote ssh only #:/home/bob:/bin/csh&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
change it to look like this&lt;br /&gt;
&lt;br /&gt;
bob:*:1001:31::0:0:remote ssh only #:/home/bob:/bin/csh&lt;br /&gt;
&lt;br /&gt;
8. At this point, if remotebob is a notebook pc he can ssh login from any place in the world. If remotebob is always working from home using ip address 192.168.1.32 you can add another level of security by restricting his login to come from that ip address. Do that by adding this statement to the Host /etc/ssh/sshd_config file&lt;br /&gt;
&lt;br /&gt;
AllowUsers bob@192.168.1.32&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Using Midnight Commander for SFTP ==&lt;br /&gt;
&lt;br /&gt;
So far the FBSD-client setup steps above have described how to use native SSH for creating the SSH keys and logging in to the FreeBSD SSHD host. Using ssh's sftp is a command line environment. Meaning you have to know all the FTP commands to navigate around the local and remote directories plus the get and put commands to really move the files between PC's. This can really be a problem for the infrequent ftp user. An command line (I.E. not X) GUI program would make this sftp directory navigation much easier. &lt;br /&gt;
&lt;br /&gt;
We are in luck. The ports system contains 'Midnight commander' an command line GUI program designed after the MS/Windows Norton Commander. It's panel has a 2 up display containing the source and target directory trees. It's normal usage is for navigating around your File system layout. But it has built in functions which allow it to do native ftp and ssh sftp between it self and the remote ftpd or sshd host. &lt;br /&gt;
&lt;br /&gt;
The current version of MC 'IE. Midnight commander' on FreeBSD 6.0 is MC 4.6.0. There is an newer version MC 4.6.1.3 in the ports which may fix the ssh login problem described later. &lt;br /&gt;
&lt;br /&gt;
You still have to run steps 1 through 5 above to create the SSH keys and moved it to the host system and then test ssh sftp login. &lt;br /&gt;
&lt;br /&gt;
After installing MC from the ports system or package system.&lt;br /&gt;
You start it be entering  mc  on the command line.&lt;br /&gt;
This brings up the GUI panel. There are 2 ways to use MC for ftp or sftp.&lt;br /&gt;
&lt;br /&gt;
One way uses the different menu panels and allows you to save the login information as a bookmark. Use F9 to active the &lt;br /&gt;
&amp;quot;Left file command options right&amp;quot; menus at the top of the MC panel. &lt;br /&gt;
The left &amp;amp; right pull down menu option have the FTP link &amp;amp; SH link option.&lt;br /&gt;
&lt;br /&gt;
The second way is the short cut method. You just enter an &amp;quot;cd&amp;quot; change directory command on the MC command line.&lt;br /&gt;
&lt;br /&gt;
cd /#sh:user@remote Host&lt;br /&gt;
&lt;br /&gt;
user = the name of the user account on the host. In this example (bob) &lt;br /&gt;
remote host = the FQDN or IP address of the host &lt;br /&gt;
&lt;br /&gt;
The MC command line at the bottom of the panel asks for the SSH pass phrase.&lt;br /&gt;
After entering the pass phrase, the remote hosts directory tree is displayed with a heading of the /#sh: command used to launched it.&lt;br /&gt;
&lt;br /&gt;
After entering the pass phrase the MC command line does not clear and ends up containing some left over parts of the SSH message. This is a bug. &lt;br /&gt;
&lt;br /&gt;
Holding down the CTL key while pressing the keyboard letter C and O keys at the same time, 2 times in a role will clear the MC command line.&lt;br /&gt;
&lt;br /&gt;
When your finished doing SSH sftp through MC, terminate the MC SSH session with the remote host by hitting F10 key.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Win-client Setup Steps ==&lt;br /&gt;
&lt;br /&gt;
There are two MS/Windows SSH client programs. The &amp;quot;Putty&amp;quot; program which provides basic SSH access for Telnet style logins and native FTP access. The &amp;quot;WinSCP3&amp;quot; program which is built on &amp;quot;putty&amp;quot; underling code that provides a Norton commander GUI style SFTP access. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Putty Setup Steps&lt;br /&gt;
1. You have to get windows putty program from&lt;br /&gt;
   http://www.chiark.greenend.org.uk/~sgtatham/putty/&lt;br /&gt;
   Download the current windows installer program.&lt;br /&gt;
   Currently that’s putty-0.58-installer.exe&lt;br /&gt;
&lt;br /&gt;
2. Run the putty-0.58-installer.exe and take all the defaults.&lt;br /&gt;
&lt;br /&gt;
3. Start/programs/Putty/Puttygen&lt;br /&gt;
   This will create your private and public ssh keys&lt;br /&gt;
   At the bottom of the window select ssh2-rsa&lt;br /&gt;
   Click on generate button.&lt;br /&gt;
   After keys are created then enter your pass phrase.&lt;br /&gt;
   Then click on ‘save public key’ button.&lt;br /&gt;
   Save it to c:/programs/putty/putty_public_rsa_key&lt;br /&gt;
   Then click on ‘save private key’ button.&lt;br /&gt;
   Save it to c:/programs/putty/putty_private_rsa_key.ppk&lt;br /&gt;
   In the window area labeled&lt;br /&gt;
   &amp;quot;Public key for pasting into Open SSH authorized_key file&amp;quot;&lt;br /&gt;
   Highlight that content and past it into a notepad text&lt;br /&gt;
   file named win_authorized_keys2.txt&lt;br /&gt;
&lt;br /&gt;
4. The win_authorized_keys2.txt file has to be sent to the&lt;br /&gt;
   Host system and put in the ~/home/bob/.ssh/ directory.&lt;br /&gt;
   In this example both the FBSD-client &amp;amp; the WIN-client will&lt;br /&gt;
   both use the same Host user account to ssh login to.&lt;br /&gt;
   Since the ~/home/bob/.ssh/authorized_keys2 already contains the&lt;br /&gt;
   private key for the FBSD-client you have to append the&lt;br /&gt;
   win_authorized_keys2.txt content to it.&lt;br /&gt;
&lt;br /&gt;
   cat win_authorized_keys2.txt &amp;gt;&amp;gt; authorized_keys2&lt;br /&gt;
&lt;br /&gt;
   If bob is only to be used by WIN-client then just&lt;br /&gt;
&lt;br /&gt;
   cp win_authorized_keys2.txt authorized_keys2&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
5. start/programs/putty/putty to run it&lt;br /&gt;
   On the bottom of the window click on &amp;quot;never exit&amp;quot;&lt;br /&gt;
   Under protocol be sure ssh is clicked.&lt;br /&gt;
   On the left side click on ‘SSH’, then in the&lt;br /&gt;
   ‘preferred ssh protocol version’ click on ‘2 only’.&lt;br /&gt;
   On the left side click on ‘AUTH’, remove check mark from&lt;br /&gt;
   ‘Attempt keyboard-interactive auth (SSH-2).&lt;br /&gt;
   On the left side click on ‘Sessions’ to return to main login window.&lt;br /&gt;
   Enter you host name (IE; your official registered domain name of&lt;br /&gt;
   the Host box) or it’s IP address. Then type a name in the saved&lt;br /&gt;
   session window and click on save button. Now click on the open button&lt;br /&gt;
   at bottom of window to login to the host.&lt;br /&gt;
&lt;br /&gt;
   A ms/dos window will open up and you will receive ‘login as’ prompt &lt;br /&gt;
   Enter bob&lt;br /&gt;
   Then you get&lt;br /&gt;
   Authenticating with public key &amp;quot;rsa-key-20060209&amp;quot;&lt;br /&gt;
   Passphrase for key &amp;quot;rsa-key-20060209&amp;quot;:&lt;br /&gt;
   At this point enter the pass phrase you entered when you ran &lt;br /&gt;
   Start/programs/Putty/Puttygen to create the rsa keys on&lt;br /&gt;
   the client remotetom.&lt;br /&gt;
&lt;br /&gt;
6. You are now logged into you host box using SSH. You have all &lt;br /&gt;
   the privileges that host user bob has when you created him.&lt;br /&gt;
   Every time you start/programs/putty/putty highlight the name&lt;br /&gt;
   of your saved session and click on the load button. This will &lt;br /&gt;
   restore your saved session configuration, then click open to launch.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== WinSCP3 Setup Steps ==&lt;br /&gt;
 &lt;br /&gt;
1. You have to get windows WinSCP3 program from&lt;br /&gt;
   http://winscp.net/eng/index.php&lt;br /&gt;
   Download the windows installer program.&lt;br /&gt;
   Currently that's winscp380setup.exe&lt;br /&gt;
&lt;br /&gt;
2. Run winscp380setup.exe and take all the defaults.&lt;br /&gt;
&lt;br /&gt;
3. Start/programs/WinSCP3/keytools/puttygen&lt;br /&gt;
   This will create your private and public ssh keys &lt;br /&gt;
&lt;br /&gt;
   At the bottom of the window select ssh2-rsa&lt;br /&gt;
   Click on generate button.&lt;br /&gt;
&lt;br /&gt;
   After keys are created then enter your pass phrase.&lt;br /&gt;
&lt;br /&gt;
   Then click on 'save public key' button.&lt;br /&gt;
   Save it to c:/Program Files/WinSCP3/putty_public_rsa_key&lt;br /&gt;
&lt;br /&gt;
   Then click on 'save private key' button.&lt;br /&gt;
   Save it to c:/Program Files/WinSCP3/putty_private_rsa_key.ppk&lt;br /&gt;
&lt;br /&gt;
   In the window area labeled &lt;br /&gt;
   &amp;quot;Public key for pasting into Open SSH authorized_key file&amp;quot; &lt;br /&gt;
   Highlight that content and past it into a notepad text file&lt;br /&gt;
   named win_authorized_keys2.txt&lt;br /&gt;
&lt;br /&gt;
4. The win_authorized_keys2.txt file has to be sent to the Host system &lt;br /&gt;
   and put in the ~/home/bob/.ssh/ directory. In this example both &lt;br /&gt;
   the FBSD-client &amp;amp; the WIN-client will both use the same Host user &lt;br /&gt;
   account to ssh login to. &lt;br /&gt;
&lt;br /&gt;
   Since the ~/home/bob/.ssh/authorized_keys2 already contains the&lt;br /&gt;
   private key for the FBSD-client you have to append the&lt;br /&gt;
   win_authorized_keys2.txt content to it.&lt;br /&gt;
&lt;br /&gt;
    cat win_authorized_keys2.txt &amp;gt;&amp;gt; authorized_keys2&lt;br /&gt;
&lt;br /&gt;
   If bob is to only be used by WIN-client then just &lt;br /&gt;
      cp win_authorized_keys2.txt authorized_keys2&lt;br /&gt;
&lt;br /&gt;
5. Start/programs/WinSCP3/WinSCP3&lt;br /&gt;
   In the window on the left, click on SSH&lt;br /&gt;
   Then for preferred ssh protocol version, click on &amp;quot;2 only&amp;quot;&lt;br /&gt;
&lt;br /&gt;
   In the window on the left, click on session&lt;br /&gt;
   Then fill in your host name FQDN or IP address.&lt;br /&gt;
   Then the user name of the host account you want to login to.&lt;br /&gt;
   Then fill in the location of for the rsa putty key created&lt;br /&gt;
   in step 3 above&lt;br /&gt;
   c:/Program Files/WinSCP3/putty_private_rsa_key.ppk&lt;br /&gt;
&lt;br /&gt;
   For protocol click on sftp &lt;br /&gt;
&lt;br /&gt;
   At the bottom of the panel click on the save button&lt;br /&gt;
   Accept the name in the pop up window or enter your own&lt;br /&gt;
   value and click OK.&lt;br /&gt;
&lt;br /&gt;
   The stored session panel opens, highlight the session name you want &lt;br /&gt;
   to run and click on the load button. This fills in the session panel&lt;br /&gt;
   with your saved configuration.&lt;br /&gt;
&lt;br /&gt;
   Click on login button to initiate your sftp session with the host.&lt;br /&gt;
   A pop up window will show asking you to enter the pass phrase you &lt;br /&gt;
   entered in &amp;quot;Step 3&amp;quot; above. Click on OK after entering you pass phrase.&lt;br /&gt;
&lt;br /&gt;
   The first time you SSH to the Host you will get these messages. &lt;br /&gt;
   Answer yes if you are sure this first connection is with your&lt;br /&gt;
   targeted host.&lt;br /&gt;
&lt;br /&gt;
   The authenticity of host '10.0.10.2 (10.0.10.2)' can't be established.&lt;br /&gt;
   DSA key fingerprint is 67:e7:90:04:0e:27:2e:d2.&lt;br /&gt;
   Are you sure you want to continue connecting (yes/no)? yes&lt;br /&gt;
   Warning: Permanently added 10.0.10.2 (DSA) to the list of known hosts.&lt;br /&gt;
&lt;br /&gt;
   The GUI Norton commander style panel will display.&lt;br /&gt;
   Left side shows the client's directory and the &lt;br /&gt;
   right side shows the Host user accounts directory &lt;br /&gt;
&lt;br /&gt;
   Every time you start/programs/WinSCP3/WinSCP3 highlight the name &lt;br /&gt;
   of your saved session and click on the load button. This will&lt;br /&gt;
   restore your saved session configuration, then click login to launch.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category : Common Tasks]]&lt;br /&gt;
[[Category : Securing FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Example_of_configuring_OpenSSH</id>
		<title>Example of configuring OpenSSH</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Example_of_configuring_OpenSSH"/>
				<updated>2012-08-13T23:11:23Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: Created page with &amp;quot;  == Example of configuring OpenSSH == Normally, when using native telnet, rlogin, or ftp your login ID, password and tramsmitted data is sent over the network in an clear, un...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Example of configuring OpenSSH ==&lt;br /&gt;
Normally, when using native telnet, rlogin, or ftp your login ID, password and tramsmitted data is sent over the network in an clear, un-encrypted form. Network sniffers anywhere in between the client and server can steal your Login information and/or data transferred in your session. &lt;br /&gt;
&lt;br /&gt;
OpenSSH encrypts all traffic (including login ID and passwords) to effectively eliminate eavesdropping, connection hijacking, and other network-level attacks. Additionally, OpenSSH provides a myriad of secure tunneling capabilities, as well as a variety of authentication methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example Environment Description ==&lt;br /&gt;
&lt;br /&gt;
In this example we have a FreeBSD system which we will call the host. This is the system that remote users want to have login access to.&lt;br /&gt;
&lt;br /&gt;
We have an Remote FreeBSD system which is located some where on the public internet, we will call this the FBSD-client. &lt;br /&gt;
&lt;br /&gt;
We also have an Remote MS/windows system which is located some where on the public internet, we will call this the Win-client.&lt;br /&gt;
&lt;br /&gt;
This example details the encrypted Host/Client key with passphrase authentication method. This method gives the maximum protection possible utilizing ssh. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Host Setup Steps ==&lt;br /&gt;
&lt;br /&gt;
1.  Edit /etc/rc.conf and add this statement&lt;br /&gt;
         sshd_enable=&amp;quot;YES&amp;quot;&lt;br /&gt;
     Make sure your firewall allows port 22 in from the public internet.&lt;br /&gt;
     Reboot your system to activate sshd and login as root.&lt;br /&gt;
     If this is your first time booting with sshd you will have to&lt;br /&gt;
     create the host keys.&lt;br /&gt;
     sshd will show you this on the first sshd boot only.&lt;br /&gt;
&lt;br /&gt;
    ====================================================&lt;br /&gt;
    Type a full screen full of random junk to unblock&lt;br /&gt;
    it and remember to finish with &amp;lt;enter&amp;gt;. This will&lt;br /&gt;
    timeout in 300 seconds, but waiting for&lt;br /&gt;
    the timeout without typing junk may make the&lt;br /&gt;
    entropy source deliver predictable output.&lt;br /&gt;
    Just hit &amp;lt;enter&amp;gt; for fast+insecure startup.&lt;br /&gt;
    ====================================================&lt;br /&gt;
    kern.random.sys.seeded: 1 -&amp;gt; 0&lt;br /&gt;
    qkcir83,2jsn40pl722jjbqok ---- this is the example junk entered&lt;br /&gt;
    Generating public/private rsa1 key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    ed:5d:97:dc:49:98:36:66:fc root@domainname&lt;br /&gt;
    Generating public/private dsa key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_dsa_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    67:e7:90:04:0e:27:2e:d2:97:6a root@a1poweruser.com&lt;br /&gt;
    Generating public/private rsa key pair.&lt;br /&gt;
    Your identification has been saved in /etc/ssh/ssh_host_rsa_key.&lt;br /&gt;
    Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    96:db:50:5c:9e:69:88:26:28:54 root@domainname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2.  If you do a &amp;quot;ps ax&amp;quot; command you will see sshd as one &lt;br /&gt;
    of the running tasks.&lt;br /&gt;
&lt;br /&gt;
3.  Using adduser or pw command create a normal user account.&lt;br /&gt;
    For this example we will use bob as the host user account name.&lt;br /&gt;
&lt;br /&gt;
4.  Hit alt/F2 at same time to open second session and login using bob.&lt;br /&gt;
&lt;br /&gt;
    Run this command ssh-keygen -t rsa&lt;br /&gt;
&lt;br /&gt;
    Just hit enter to take default location and file name&lt;br /&gt;
    No need to enter a pass phrase for the host user here,&lt;br /&gt;
    just hit enter 2 times.&lt;br /&gt;
    This is what you will see&lt;br /&gt;
&lt;br /&gt;
    Generating public/private rsa key pair.&lt;br /&gt;
    Enter file in which to save the key (/bob/.ssh/id_rsa):&lt;br /&gt;
    Created directory '/bob/.ssh'.&lt;br /&gt;
    Enter passphrase (empty for no passphrase):&lt;br /&gt;
    Enter same passphrase again:&lt;br /&gt;
    Your identification has been saved in /bob/.ssh/id_rsa.&lt;br /&gt;
    Your public key has been saved in /bob/.ssh/id_rsa.pub.&lt;br /&gt;
    The key fingerprint is:&lt;br /&gt;
    e7:e6:8f:d3:b1:b4:08:27:09:d2 bob@domainname&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
5.  If you want to use the Host 'root' account to initiate a ssh&lt;br /&gt;
    session with an remote system you have to perform step 4 above.&lt;br /&gt;
    If you want to do remote ssh login to this Host as user ‘root’, you&lt;br /&gt;
    have to edit /etc/ssh/sshd_config on the this Host and change;&lt;br /&gt;
&lt;br /&gt;
    #PermitRootLogin no  to  PermitRootLogin yes &lt;br /&gt;
&lt;br /&gt;
    Then /etc/rc.d/sshd reload  &lt;br /&gt;
&lt;br /&gt;
    to make sshd task reread it’s sshd_config file. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FBSD-client Setup Steps ==&lt;br /&gt;
&lt;br /&gt;
1. Using adduser or pw command create a normal user account.&lt;br /&gt;
   For this example we will use remotetom as the user account name.&lt;br /&gt;
&lt;br /&gt;
2. Login using remotetom.&lt;br /&gt;
   Run this command ssh-keygen -t rsa&lt;br /&gt;
&lt;br /&gt;
3. Just hit enter to take default location and file name.&lt;br /&gt;
   At the &amp;quot;Enter a passphrase prompt&amp;quot; [enter one and write it down, &lt;br /&gt;
   because it will be needed for ssh login to the host].&lt;br /&gt;
   This is what you will see&lt;br /&gt;
&lt;br /&gt;
   Generating public/private rsa key pair.&lt;br /&gt;
   Enter file in which to save the key (/remotetom/.ssh/id_rsa):&lt;br /&gt;
   Created directory '/remotetom/.ssh'.&lt;br /&gt;
   Enter passphrase (empty for no passphrase):&lt;br /&gt;
   Enter same passphrase again:&lt;br /&gt;
   Your identification has been saved in /remotetom/.ssh/id_rsa.&lt;br /&gt;
   Your public key has been saved in /remotetom/.ssh/id_rsa.pub.&lt;br /&gt;
   The key fingerprint is:&lt;br /&gt;
   e7:e6:8f:d3:b1:b4:08:27:09:56:de:d2 tom@domainname &lt;br /&gt;
&lt;br /&gt;
4. The Public key file you just created /remotetom/.ssh/id_rsa.pub &lt;br /&gt;
   has to be sent to the Host system. On the host system rename it&lt;br /&gt;
   to authorized_keys2 and put it into the home directory of the &lt;br /&gt;
   user setup earlier. In this case ~/bob/.ssh/authorized_keys2. &lt;br /&gt;
&lt;br /&gt;
5. To ssh to the host, enter this ssh hostname or ssh host-ip-address&lt;br /&gt;
  The first time you ssh to the Host you will get these messages.&lt;br /&gt;
  Answer yes if you are sure this first connection is with your&lt;br /&gt;
  targeted host. Enter exit to terminate session.&lt;br /&gt;
&lt;br /&gt;
   # /home/remotetom/.ssh &amp;gt;ssh 10.0.10.2&lt;br /&gt;
   The authenticity of host '10.0.10.2 (10.0.10.2)' can't be established.&lt;br /&gt;
   DSA key fingerprint is 04:0e:27:2e:d2:97:9d:7b:62:7e:c9:32:6a.&lt;br /&gt;
   Are you sure you want to continue connecting (yes/no)? yes&lt;br /&gt;
   Warning: Permanently added '10.0.10.2' (DSA) to list of known hosts.&lt;br /&gt;
   Enter passphrase for key /remotetom/.ssh/id_rsa&lt;br /&gt;
&lt;br /&gt;
   This is where you enter the pass phrase you entered while running &lt;br /&gt;
   ssh-keygen on this remote client. (remotetom)&lt;br /&gt;
&lt;br /&gt;
6. You are now logged into the Host account bob.&lt;br /&gt;
   Every time you ssh login again you will just get this prompt&lt;br /&gt;
&lt;br /&gt;
   Enter passphrase for key /remotetom/.ssh/id_rsa&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
7. At this point the Host user bob can login from Host console, or using telnet from a local LAN machine, or from the public internet if your host firewall has port 23 open. If this user is to be only allowed remote ssh client access you can use vipw command to edit the system’s master password file and star out his encrypted password. This will deny login from anywhere except remotebob&lt;br /&gt;
&lt;br /&gt;
bob:$1$FXF.CEpf:1001:31::0:0:remote ssh only #:/home/bob:/bin/csh&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
change it to look like this&lt;br /&gt;
&lt;br /&gt;
bob:*:1001:31::0:0:remote ssh only #:/home/bob:/bin/csh&lt;br /&gt;
&lt;br /&gt;
8. At this point, if remotebob is a notebook pc he can ssh login from any place in the world. If remotebob is always working from home using ip address 192.168.1.32 you can add another level of security by restricting his login to come from that ip address. Do that by adding this statement to the Host /etc/ssh/sshd_config file&lt;br /&gt;
&lt;br /&gt;
AllowUsers bob@192.168.1.32&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Using Midnight Commander for SFTP ==&lt;br /&gt;
&lt;br /&gt;
So far the FBSD-client setup steps above have described how to use native SSH for creating the SSH keys and logging in to the FreeBSD SSHD host. Using ssh's sftp is a command line environment. Meaning you have to know all the FTP commands to navigate around the local and remote directories plus the get and put commands to really move the files between PC's. This can really be a problem for the infrequent ftp user. An command line (I.E. not X) GUI program would make this sftp directory navigation much easier. &lt;br /&gt;
&lt;br /&gt;
We are in luck. The ports system contains 'Midnight commander' an command line GUI program designed after the MS/Windows Norton Commander. It's panel has a 2 up display containing the source and target directory trees. It's normal usage is for navigating around your File system layout. But it has built in functions which allow it to do native ftp and ssh sftp between it self and the remote ftpd or sshd host. &lt;br /&gt;
&lt;br /&gt;
The current version of MC 'IE. Midnight commander' on FreeBSD 6.0 is MC 4.6.0. There is an newer version MC 4.6.1.3 in the ports which may fix the ssh login problem described later. &lt;br /&gt;
&lt;br /&gt;
You still have to run steps 1 through 5 above to create the SSH keys and moved it to the host system and then test ssh sftp login. &lt;br /&gt;
&lt;br /&gt;
After installing MC from the ports system or package system.&lt;br /&gt;
You start it be entering  mc  on the command line.&lt;br /&gt;
This brings up the GUI panel. There are 2 ways to use MC for ftp or sftp.&lt;br /&gt;
&lt;br /&gt;
One way uses the different menu panels and allows you to save the login information as a bookmark. Use F9 to active the &lt;br /&gt;
&amp;quot;Left file command options right&amp;quot; menus at the top of the MC panel. &lt;br /&gt;
The left &amp;amp; right pull down menu option have the FTP link &amp;amp; SH link option.&lt;br /&gt;
&lt;br /&gt;
The second way is the short cut method. You just enter an &amp;quot;cd&amp;quot; change directory command on the MC command line.&lt;br /&gt;
&lt;br /&gt;
cd /#sh:user@remote Host&lt;br /&gt;
&lt;br /&gt;
user = the name of the user account on the host. In this example (bob) &lt;br /&gt;
remote host = the FQDN or IP address of the host &lt;br /&gt;
&lt;br /&gt;
The MC command line at the bottom of the panel asks for the SSH pass phrase.&lt;br /&gt;
After entering the pass phrase, the remote hosts directory tree is displayed with a heading of the /#sh: command used to launched it.&lt;br /&gt;
&lt;br /&gt;
After entering the pass phrase the MC command line does not clear and ends up containing some left over parts of the SSH message. This is a bug. &lt;br /&gt;
&lt;br /&gt;
Holding down the CTL key while pressing the keyboard letter C and O keys at the same time, 2 times in a role will clear the MC command line.&lt;br /&gt;
&lt;br /&gt;
When your finished doing SSH sftp through MC, terminate the MC SSH session with the remote host by hitting F10 key.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Win-client Setup Steps ==&lt;br /&gt;
&lt;br /&gt;
There are two MS/Windows SSH client programs. The &amp;quot;Putty&amp;quot; program which provides basic SSH access for Telnet style logins and native FTP access. The &amp;quot;WinSCP3&amp;quot; program which is built on &amp;quot;putty&amp;quot; underling code that provides a Norton commander GUI style SFTP access. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Putty Setup Steps&lt;br /&gt;
1. You have to get windows putty program from&lt;br /&gt;
   http://www.chiark.greenend.org.uk/~sgtatham/putty/&lt;br /&gt;
   Download the current windows installer program.&lt;br /&gt;
   Currently that’s putty-0.58-installer.exe&lt;br /&gt;
&lt;br /&gt;
2. Run the putty-0.58-installer.exe and take all the defaults.&lt;br /&gt;
&lt;br /&gt;
3. Start/programs/Putty/Puttygen&lt;br /&gt;
   This will create your private and public ssh keys&lt;br /&gt;
   At the bottom of the window select ssh2-rsa&lt;br /&gt;
   Click on generate button.&lt;br /&gt;
   After keys are created then enter your pass phrase.&lt;br /&gt;
   Then click on ‘save public key’ button.&lt;br /&gt;
   Save it to c:/programs/putty/putty_public_rsa_key&lt;br /&gt;
   Then click on ‘save private key’ button.&lt;br /&gt;
   Save it to c:/programs/putty/putty_private_rsa_key.ppk&lt;br /&gt;
   In the window area labeled&lt;br /&gt;
   &amp;quot;Public key for pasting into Open SSH authorized_key file&amp;quot;&lt;br /&gt;
   Highlight that content and past it into a notepad text&lt;br /&gt;
   file named win_authorized_keys2.txt&lt;br /&gt;
&lt;br /&gt;
4. The win_authorized_keys2.txt file has to be sent to the&lt;br /&gt;
   Host system and put in the ~/home/bob/.ssh/ directory.&lt;br /&gt;
   In this example both the FBSD-client &amp;amp; the WIN-client will&lt;br /&gt;
   both use the same Host user account to ssh login to.&lt;br /&gt;
   Since the ~/home/bob/.ssh/authorized_keys2 already contains the&lt;br /&gt;
   private key for the FBSD-client you have to append the&lt;br /&gt;
   win_authorized_keys2.txt content to it.&lt;br /&gt;
&lt;br /&gt;
   cat win_authorized_keys2.txt &amp;gt;&amp;gt; authorized_keys2&lt;br /&gt;
&lt;br /&gt;
   If bob is only to be used by WIN-client then just&lt;br /&gt;
&lt;br /&gt;
   cp win_authorized_keys2.txt authorized_keys2&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
5. start/programs/putty/putty to run it&lt;br /&gt;
   On the bottom of the window click on &amp;quot;never exit&amp;quot;&lt;br /&gt;
   Under protocol be sure ssh is clicked.&lt;br /&gt;
   On the left side click on ‘SSH’, then in the&lt;br /&gt;
   ‘preferred ssh protocol version’ click on ‘2 only’.&lt;br /&gt;
   On the left side click on ‘AUTH’, remove check mark from&lt;br /&gt;
   ‘Attempt keyboard-interactive auth (SSH-2).&lt;br /&gt;
   On the left side click on ‘Sessions’ to return to main login window.&lt;br /&gt;
   Enter you host name (IE; your official registered domain name of&lt;br /&gt;
   the Host box) or it’s IP address. Then type a name in the saved&lt;br /&gt;
   session window and click on save button. Now click on the open button&lt;br /&gt;
   at bottom of window to login to the host.&lt;br /&gt;
&lt;br /&gt;
   A ms/dos window will open up and you will receive ‘login as’ prompt &lt;br /&gt;
   Enter bob&lt;br /&gt;
   Then you get&lt;br /&gt;
   Authenticating with public key &amp;quot;rsa-key-20060209&amp;quot;&lt;br /&gt;
   Passphrase for key &amp;quot;rsa-key-20060209&amp;quot;:&lt;br /&gt;
   At this point enter the pass phrase you entered when you ran &lt;br /&gt;
   Start/programs/Putty/Puttygen to create the rsa keys on&lt;br /&gt;
   the client remotetom.&lt;br /&gt;
&lt;br /&gt;
6. You are now logged into you host box using SSH. You have all &lt;br /&gt;
   the privileges that host user bob has when you created him.&lt;br /&gt;
   Every time you start/programs/putty/putty highlight the name&lt;br /&gt;
   of your saved session and click on the load button. This will &lt;br /&gt;
   restore your saved session configuration, then click open to launch.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== WinSCP3 Setup Steps ==&lt;br /&gt;
 &lt;br /&gt;
1. You have to get windows WinSCP3 program from&lt;br /&gt;
   http://winscp.net/eng/index.php&lt;br /&gt;
   Download the windows installer program.&lt;br /&gt;
   Currently that's winscp380setup.exe&lt;br /&gt;
&lt;br /&gt;
2. Run winscp380setup.exe and take all the defaults.&lt;br /&gt;
&lt;br /&gt;
3. Start/programs/WinSCP3/keytools/puttygen&lt;br /&gt;
   This will create your private and public ssh keys &lt;br /&gt;
&lt;br /&gt;
   At the bottom of the window select ssh2-rsa&lt;br /&gt;
   Click on generate button.&lt;br /&gt;
&lt;br /&gt;
   After keys are created then enter your pass phrase.&lt;br /&gt;
&lt;br /&gt;
   Then click on 'save public key' button.&lt;br /&gt;
   Save it to c:/Program Files/WinSCP3/putty_public_rsa_key&lt;br /&gt;
&lt;br /&gt;
   Then click on 'save private key' button.&lt;br /&gt;
   Save it to c:/Program Files/WinSCP3/putty_private_rsa_key.ppk&lt;br /&gt;
&lt;br /&gt;
   In the window area labeled &lt;br /&gt;
   &amp;quot;Public key for pasting into Open SSH authorized_key file&amp;quot; &lt;br /&gt;
   Highlight that content and past it into a notepad text file&lt;br /&gt;
   named win_authorized_keys2.txt&lt;br /&gt;
&lt;br /&gt;
4. The win_authorized_keys2.txt file has to be sent to the Host system &lt;br /&gt;
   and put in the ~/home/bob/.ssh/ directory. In this example both &lt;br /&gt;
   the FBSD-client &amp;amp; the WIN-client will both use the same Host user &lt;br /&gt;
   account to ssh login to. &lt;br /&gt;
&lt;br /&gt;
   Since the ~/home/bob/.ssh/authorized_keys2 already contains the&lt;br /&gt;
   private key for the FBSD-client you have to append the&lt;br /&gt;
   win_authorized_keys2.txt content to it.&lt;br /&gt;
&lt;br /&gt;
    cat win_authorized_keys2.txt &amp;gt;&amp;gt; authorized_keys2&lt;br /&gt;
&lt;br /&gt;
   If bob is to only be used by WIN-client then just &lt;br /&gt;
      cp win_authorized_keys2.txt authorized_keys2&lt;br /&gt;
&lt;br /&gt;
5. Start/programs/WinSCP3/WinSCP3&lt;br /&gt;
   In the window on the left, click on SSH&lt;br /&gt;
   Then for preferred ssh protocol version, click on &amp;quot;2 only&amp;quot;&lt;br /&gt;
&lt;br /&gt;
   In the window on the left, click on session&lt;br /&gt;
   Then fill in your host name FQDN or IP address.&lt;br /&gt;
   Then the user name of the host account you want to login to.&lt;br /&gt;
   Then fill in the location of for the rsa putty key created&lt;br /&gt;
   in step 3 above&lt;br /&gt;
   c:/Program Files/WinSCP3/putty_private_rsa_key.ppk&lt;br /&gt;
&lt;br /&gt;
   For protocol click on sftp &lt;br /&gt;
&lt;br /&gt;
   At the bottom of the panel click on the save button&lt;br /&gt;
   Accept the name in the pop up window or enter your own&lt;br /&gt;
   value and click OK.&lt;br /&gt;
&lt;br /&gt;
   The stored session panel opens, highlight the session name you want &lt;br /&gt;
   to run and click on the load button. This fills in the session panel&lt;br /&gt;
   with your saved configuration.&lt;br /&gt;
&lt;br /&gt;
   Click on login button to initiate your sftp session with the host.&lt;br /&gt;
   A pop up window will show asking you to enter the pass phrase you &lt;br /&gt;
   entered in &amp;quot;Step 3&amp;quot; above. Click on OK after entering you pass phrase.&lt;br /&gt;
&lt;br /&gt;
   The first time you SSH to the Host you will get these messages. &lt;br /&gt;
   Answer yes if you are sure this first connection is with your&lt;br /&gt;
   targeted host.&lt;br /&gt;
&lt;br /&gt;
   The authenticity of host '10.0.10.2 (10.0.10.2)' can't be established.&lt;br /&gt;
   DSA key fingerprint is 67:e7:90:04:0e:27:2e:d2.&lt;br /&gt;
   Are you sure you want to continue connecting (yes/no)? yes&lt;br /&gt;
   Warning: Permanently added 10.0.10.2 (DSA) to the list of known hosts.&lt;br /&gt;
&lt;br /&gt;
   The GUI Norton commander style panel will display.&lt;br /&gt;
   Left side shows the client's directory and the &lt;br /&gt;
   right side shows the Host user accounts directory &lt;br /&gt;
&lt;br /&gt;
   Every time you start/programs/WinSCP3/WinSCP3 highlight the name &lt;br /&gt;
   of your saved session and click on the load button. This will&lt;br /&gt;
   restore your saved session configuration, then click login to launch.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category : Common Tasks]]&lt;br /&gt;
[[Category : Securing FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Defending_Against_Attacks</id>
		<title>Defending Against Attacks</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Defending_Against_Attacks"/>
				<updated>2012-08-13T23:00:41Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Defending Against Attacks ==&lt;br /&gt;
A firewall is your first line of defense, But if you allow public access to ports 22, SSH (secure shell), 23, Telnet, or 21, FTP these ports can be bombarded with login attempts using common ID/PW combinations probing for access. In the case of port 80, Web server it can be bombarded with access requests designed to consume server resources resulting in a denial of service to legitimate user requests. To the firewall these all look like legitimate packets.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Know Who Your Attacker is ==&lt;br /&gt;
98 percent of the attackes come from script kiddies or robots. These attacks are all most totally based on indiscriminate rolling through a range of sequential IP address. (IE: They never use DNS to lookup your domain name.) You were found by plain bad luck. These attacks most offten only address the know port numbers listened on by those services. You use this knowledge to defend against this type of attack.&lt;br /&gt;
&lt;br /&gt;
The most common program used to probe is nmap. It's even in the ports system as, security/nmap. Nmap is a utility for network exploration or security auditing. It supports ping scanning (determine which hosts are up), many port scanning techniques, version detection (determine service protocols and application versions listening behind ports), and TCP/IP fingerprinting (remote host OS or device identification). Nmap also offers flexible target and port specification, decoy/stealth scanning, sunRPC scanning, and more.&lt;br /&gt;
&lt;br /&gt;
Check out http://www.insecure.org/nmap/ for more details.&lt;br /&gt;
&lt;br /&gt;
Read Phrack Magazine article September 01, 1997, for overview of the different probing techniques http://www.insecure.org/nmap/p51-11.txt&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The simplest defense is to change the port numbers these services use. The /etc/services is where SSH, Telnet, and FTP port numbers are defined and where you would change them at. (Note: Your firewall should only allow passive FTP by not opening high value port number for active FTP data transmission.) For Apache web server you specify the access port number in httpd.conf definitions.&lt;br /&gt;
&lt;br /&gt;
Remote clients who want to access your public services on the alternate port number will have to enter the alternate port number as part of the login command. After setting up alternate port numbers you can have your firewall log all access to ports 21,22,23,or 80 and report the abuse to the ISP owner of the sending IP address using the FreeBSD port ppars-1.0   See Abuse reporting system&lt;br /&gt;
&lt;br /&gt;
Or if you don't want to use the automated Abuse reporting system you can take the sending IP address from your firewall log and do manual whois command to find the ISP owner of the offending IP address along with the ISP's abuse reporting email address and send your own email to them about their client sending you attack packets.&lt;br /&gt;
&lt;br /&gt;
Installers Note: The technique of assigning non-standard ports for SSH, Telnet, and FTP is security through obscurity and is one of the first and easiest security measures an administrator may consider. By no means does this provide protection from the dedicated attacker who has targeted your IP address for some reason. This is just the first security level of hiding through obscurity from the majority of the high school / college freshmen script kiddies who really don't know what they are doing yet. You will be surprised at how effective this really is at discouraging repeat port scan attacks and fraudulent login attempts.   &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stopping Login Attacks ==&lt;br /&gt;
Using the customary port numbers or alternate port numbers for SSH, FTP, or Telnet all failed logins are logged to /var/log/auth.log file. In most cases the sending IP address is the real IP address of the attacker. In the long term the solution is to do whois on the attackers IP address and report him to the ISP who owns the IP address. &lt;br /&gt;
&lt;br /&gt;
In the short term to stop the login attack in progress many people will add a &amp;quot;deny this IP address&amp;quot; rule to their firewall rule set file. Yes this will stop the attack immediately, but when a firewall keeps all these special &amp;quot;deny this IP address&amp;quot; rules the firewall becomes very hard to maintain as that list of denied IP address rules grows longer. A far better solution is to separate the denied IP address list from the firewall rule set. This can be done using the &amp;quot;routed blackhole&amp;quot; command.&lt;br /&gt;
&lt;br /&gt;
Example:   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
To Add use      route add -host attacker_ip 127.0.0.1 -blackhole&lt;br /&gt;
&lt;br /&gt;
To Delete use   route delete -host attacker_ip 127.0.0.1 -blackhole&lt;br /&gt;
&lt;br /&gt;
To List use     netstat -nr|grep 127&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is executed in the IP stack and is faster than in the firewall when you have over 20 of those special &amp;quot;deny this IP address&amp;quot; rules in the firewall. The &amp;quot;attacker_ip&amp;quot; in found in the log records in /var/log/auth.log file.&lt;br /&gt;
&lt;br /&gt;
You can create a script (route_blackholed_ip.sh) containing route commands for all the IP address that have attacked you in the past and save it to /usr/local/etc/rc.d/ so it will be run at boot time.&lt;br /&gt;
&lt;br /&gt;
The same process used by the abuse reporting system to process the /var/log/security log file can be modified by you to automate the processing of the /var/log/auth.log file to create the route blackhole commands on the fly while the attack is occurring.&lt;br /&gt;
&lt;br /&gt;
Installers Note: The ports system currently contains /security/bruteforceblocker and /security/sshit for automating the capturing of the fraudulent login attempts IP address and inserting firewall rules to block that offending IP address. The PF firewall has built in self limiting parameters (max-src-conn &amp;amp; max-src-conn-rate)which when execded can add the offending IP address automatically into a table of IP address that are blocked by the firewall. &lt;br /&gt;
&lt;br /&gt;
BruteForceBlocker is a perl script, that works along with pf - OpenBSD's firewall. It's main purpose is to block SSH bruteforce login attacks via firewall. When this script is running, it checks for sshd log records in the syslog and looks for failed Login attempts and counts number of such attempts. When given IP reaches configured limit of fails, script puts this IP into pf's table and blocks any further traffic to the that box from that given IP.&lt;br /&gt;
&lt;br /&gt;
sshit is a perl script, which works along with ipfw, ipf, and pf. It parses the output of syslogd, finds SSH/FTP bruteforce fraudulent login attempts. If the number of failed login attempts is greater than a threshold set by the  administrator, sshit will block the source IP via firewall for a while (administrators can set the period of blocking).&lt;br /&gt;
&lt;br /&gt;
PF Firewall Method Using the self limiting parameters max-src-conn &amp;amp; max-src-conn-rate coupled with the overload and expiretable parameters for automatic population of a table containing the offending IP address and the automatic removal of the offending IP address after no activity in a predefined time span. This method works for FTP, telnet, and ssh bruteforce logon type attacks in a simple and clean manner. IPFW &amp;amp; IPF firewalls do not have this built in capacity&lt;br /&gt;
&lt;br /&gt;
See this link for rule coding details: http://www.bgnett.no/~peter/pf/en/bruteforce.html &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stopping Web Server attacks ==&lt;br /&gt;
Web server attacks are denial of service (Dos) attacks. There is no trigger that will notify you when this occurs. Most likely your first warning something is wrong is when people start asking you why is your web server down. When you have reason to suspect your web server is under attack you can check /var/log/hpptd-access.log file. This log file gets a log record for every file accessed by your web server. Part of the log record is the requesting IP address or it's DNS name. When you see a lot of log records (in the hundreds) from the same IP address, that is your attacker. In most all cases the requesting IP address is spoofed. Spoofed means the IP address is a real public internet routable IP address belonging to a legitimate user that unknown to him, the attacker has used to hide his real identity. &lt;br /&gt;
&lt;br /&gt;
Like with Login attacks you can add a special &amp;quot;deny this IP address&amp;quot; rule to your firewall rule set file or use the &amp;quot;routed blackhole&amp;quot; command.&lt;br /&gt;
&lt;br /&gt;
The same process used by the abuse reporting system to process the /var/log/security log file can be modified by you to automate the processing of the /var/log/hpptd-access.log file to create the route blackhole commands on the fly while the attack is occurring.&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Defending_Against_Attacks</id>
		<title>Defending Against Attacks</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Defending_Against_Attacks"/>
				<updated>2012-08-13T22:55:38Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: Created page with &amp;quot;  == Defending Against Attacks == A firewall is your first line of defense, But if you allow public access to ports 22, SSH (secure shell), 23, Telnet, or 21, FTP these ports ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
== Defending Against Attacks ==&lt;br /&gt;
A firewall is your first line of defense, But if you allow public access to ports 22, SSH (secure shell), 23, Telnet, or 21, FTP these ports can be bombarded with login attempts using common ID/PW combinations probing for access. In the case of port 80, Web server it can be bombarded with access requests designed to consume server resources resulting in a denial of service to legitimate user requests. To the firewall these all look like legitimate packets.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Know Who Your Attacker is ==&lt;br /&gt;
98 percent of the attackes come from script kiddies or robots. These attacks are all most totally based on indiscriminate rolling through a range of sequential IP address. (IE: They never use DNS to lookup your domain name.) You were found by plain bad luck. These attacks most offten only address the know port numbers listened on by those services. You use this knowledge to defend against this type of attack.&lt;br /&gt;
&lt;br /&gt;
The most common program used to probe is nmap. It's even in the ports system as, security/nmap. Nmap is a utility for network exploration or security auditing. It supports ping scanning (determine which hosts are up), many port scanning techniques, version detection (determine service protocols and application versions listening behind ports), and TCP/IP fingerprinting (remote host OS or device identification). Nmap also offers flexible target and port specification, decoy/stealth scanning, sunRPC scanning, and more.&lt;br /&gt;
&lt;br /&gt;
Check out http://www.insecure.org/nmap/ for more details.&lt;br /&gt;
&lt;br /&gt;
Read Phrack Magazine article September 01, 1997, for overview of the different probing techniques http://www.insecure.org/nmap/p51-11.txt&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The simplest defense is to change the port numbers these services use. The /etc/services is where SSH, Telnet, and FTP port numbers are defined and where you would change them at. (Note: Your firewall should only allow passive FTP by not opening high value port number for active FTP data transmission.) For Apache web server you specify the access port number in httpd.conf definitions.&lt;br /&gt;
&lt;br /&gt;
Remote clients who want to access your public services on the alternate port number will have to enter the alternate port number as part of the login command. After setting up alternate port numbers you can have your firewall log all access to ports 21,22,23,or 80 and report the abuse to the ISP owner of the sending IP address using the FreeBSD port ppars-1.0   See Abuse reporting system&lt;br /&gt;
&lt;br /&gt;
Or if you don't want to use the automated Abuse reporting system you can take the sending IP address from your firewall log and do manual whois command to find the ISP owner of the offending IP address along with the ISP's abuse reporting email address and send your own email to them about their client sending you attack packets.&lt;br /&gt;
&lt;br /&gt;
Installers Note: The technique of assigning non-standard ports for SSH, Telnet, and FTP is security through obscurity and is one of the first and easiest security measures an administrator may consider. By no means does this provide protection from the dedicated attacker who has targeted your IP address for some reason. This is just the first security level of hiding through obscurity from the majority of the high school / college freshmen script kiddies who really don't know what they are doing yet. You will be surprised at how effective this really is at discouraging repeat port scan attacks and fraudulent login attempts.   &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stopping Login Attacks ==&lt;br /&gt;
Using the customary port numbers or alternate port numbers for SSH, FTP, or Telnet all failed logins are logged to /var/log/auth.log file. In most cases the sending IP address is the real IP address of the attacker. In the long term the solution is to do whois on the attackers IP address and report him to the ISP who owns the IP address. &lt;br /&gt;
&lt;br /&gt;
In the short term to stop the login attack in progress many people will add a &amp;quot;deny this IP address&amp;quot; rule to their firewall rule set file. Yes this will stop the attack immediately, but when a firewall keeps all these special &amp;quot;deny this IP address&amp;quot; rules the firewall becomes very hard to maintain as that list of denied IP address rules grows longer. A far better solution is to separate the denied IP address list from the firewall rule set. This can be done using the &amp;quot;routed blackhole&amp;quot; command.&lt;br /&gt;
&lt;br /&gt;
Example:   &lt;br /&gt;
&lt;br /&gt;
To Add use      route add -host attacker_ip 127.0.0.1 -blackhole&lt;br /&gt;
&lt;br /&gt;
To Delete use   route delete -host attacker_ip 127.0.0.1 -blackhole&lt;br /&gt;
&lt;br /&gt;
To List use     netstat -nr|grep 127&lt;br /&gt;
&lt;br /&gt;
This is executed in the IP stack and is faster than in the firewall when you have over 20 of those special &amp;quot;deny this IP address&amp;quot; rules in the firewall. The &amp;quot;attacker_ip&amp;quot; in found in the log records in /var/log/auth.log file.&lt;br /&gt;
&lt;br /&gt;
You can create a script (route_blackholed_ip.sh) containing route commands for all the IP address that have attacked you in the past and save it to /usr/local/etc/rc.d/ so it will be run at boot time.&lt;br /&gt;
&lt;br /&gt;
The same process used by the abuse reporting system to process the /var/log/security log file can be modified by you to automate the processing of the /var/log/auth.log file to create the route blackhole commands on the fly while the attack is occurring.&lt;br /&gt;
&lt;br /&gt;
Installers Note: The ports system currently contains /security/bruteforceblocker and /security/sshit for automating the capturing of the fraudulent login attempts IP address and inserting firewall rules to block that offending IP address. The PF firewall has built in self limiting parameters (max-src-conn &amp;amp; max-src-conn-rate)which when execded can add the offending IP address automatically into a table of IP address that are blocked by the firewall. &lt;br /&gt;
&lt;br /&gt;
BruteForceBlocker is a perl script, that works along with pf - OpenBSD's firewall. It's main purpose is to block SSH bruteforce login attacks via firewall. When this script is running, it checks for sshd log records in the syslog and looks for failed Login attempts and counts number of such attempts. When given IP reaches configured limit of fails, script puts this IP into pf's table and blocks any further traffic to the that box from that given IP.&lt;br /&gt;
&lt;br /&gt;
sshit is a perl script, which works along with ipfw, ipf, and pf. It parses the output of syslogd, finds SSH/FTP bruteforce fraudulent login attempts. If the number of failed login attempts is greater than a threshold set by the  administrator, sshit will block the source IP via firewall for a while (administrators can set the period of blocking).&lt;br /&gt;
&lt;br /&gt;
PF Firewall Method Using the self limiting parameters max-src-conn &amp;amp; max-src-conn-rate coupled with the overload and expiretable parameters for automatic population of a table containing the offending IP address and the automatic removal of the offending IP address after no activity in a predefined time span. This method works for FTP, telnet, and ssh bruteforce logon type attacks in a simple and clean manner. IPFW &amp;amp; IPF firewalls do not have this built in capacity&lt;br /&gt;
&lt;br /&gt;
See this link for rule coding details: http://www.bgnett.no/~peter/pf/en/bruteforce.html &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stopping Web Server attacks ==&lt;br /&gt;
Web server attacks are denial of service (Dos) attacks. There is no trigger that will notify you when this occurs. Most likely your first warning something is wrong is when people start asking you why is your web server down. When you have reason to suspect your web server is under attack you can check /var/log/hpptd-access.log file. This log file gets a log record for every file accessed by your web server. Part of the log record is the requesting IP address or it's DNS name. When you see a lot of log records (in the hundreds) from the same IP address, that is your attacker. In most all cases the requesting IP address is spoofed. Spoofed means the IP address is a real public internet routable IP address belonging to a legitimate user that unknown to him, the attacker has used to hide his real identity. &lt;br /&gt;
&lt;br /&gt;
Like with Login attacks you can add a special &amp;quot;deny this IP address&amp;quot; rule to your firewall rule set file or use the &amp;quot;routed blackhole&amp;quot; command.&lt;br /&gt;
&lt;br /&gt;
The same process used by the abuse reporting system to process the /var/log/security log file can be modified by you to automate the processing of the /var/log/hpptd-access.log file to create the route blackhole commands on the fly while the attack is occurring.&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T22:48:12Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFW Rule Sets ==&lt;br /&gt;
A rule set is a group of ipfw rules coded to allow or deny packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc.) is predefined by its protocol and port number. This is the basic selection criteria used to create rules which will allow or deny services. &lt;br /&gt;
&lt;br /&gt;
When a packet enters the firewall it is compared against the first rule in the rule set and progresses one rule at a time, moving from top to bottom of the set in ascending rule number sequence order. When the packet matches a rule selection parameter, the rule's action field value is executed and the search of the rule set terminates for that packet. This is referred to as the 'first match wins' search method. If the packet does not match any of the rules, it gets caught by the mandatory ipfw default rule, number 65535 which denies all packets and discards them without any reply back to the originating destination. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section of the Installers Guide is based on using rules that contain the stateful ‘keep state’ and ‘limit’ options. This is the basic framework for coding an inclusive type firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destine for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is denied by default design. Inclusive firewalls are much more secure than exclusive firewall rule sets and are the only rule set type covered here in. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to what is necessary to create a standard inclusive type firewall rule set. For a complete rule syntax description see the online ‘man ipfw’ page at &lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/cgi/man.cgi?query=ipfw&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at &lt;br /&gt;
the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Syntax = CMD RULE# ACTION LOGGING SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
CMD Each rule has to be prefixed with the following to add the rule to the internal table,&lt;br /&gt;
&lt;br /&gt;
ipfw add&lt;br /&gt;
&lt;br /&gt;
RULE# Coding rule numbers is not a mandatory requirement. Rule numbers will automatically be assigned when the rules are loaded into the internal IPFW tables. Coding your own rule numbers means the numbers will not change during loading and gives you a fixed rule number which is listed in the log along with other information about the packet being logged. The rule number is how you relate the logged packet back to the rule that caused the packet to be logged. If a rule is entered without a number, ipfw will assign one. &lt;br /&gt;
&lt;br /&gt;
ACTIONS&lt;br /&gt;
&lt;br /&gt;
A rule can be associated with one of the following actions which will be &lt;br /&gt;
executed when the packet matches the selection criterion of the rule.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
allow | accept | pass | permit&lt;br /&gt;
&lt;br /&gt;
       These all mean the same thing which is to allow &lt;br /&gt;
       packets that match the rule to exit the firewall &lt;br /&gt;
       rule processing. The search terminates.&lt;br /&gt;
&lt;br /&gt;
check-state&lt;br /&gt;
&lt;br /&gt;
      Checks the packet against the dynamic rules table. &lt;br /&gt;
      If a match is found, execute the action associated with &lt;br /&gt;
      the rule which generated this dynamic rule, otherwise move &lt;br /&gt;
      to the next rule. The check-state rule does not have &lt;br /&gt;
      selection criteria. If no check-state rule is present in &lt;br /&gt;
      the rule set, the dynamic rules table is checked at the &lt;br /&gt;
      first keep-state or limit rule.&lt;br /&gt;
&lt;br /&gt;
deny | drop&lt;br /&gt;
&lt;br /&gt;
     Both words mean the same thing which is to discard packets &lt;br /&gt;
     that match this rule. The search terminates.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
LOGGING&lt;br /&gt;
&lt;br /&gt;
log or logamount number&lt;br /&gt;
&lt;br /&gt;
    When a packet matches a rule with the log keyword, a message will be&lt;br /&gt;
    logged to syslogd with a facility name of SECURITY. The logging &lt;br /&gt;
    only occurs if the number of packets logged so far for that&lt;br /&gt;
    particular rule does not exceed the logamount parameter. If no&lt;br /&gt;
    logamount is specified, the limit is taken from the sysctl variable&lt;br /&gt;
    net.inet.ip.fw.verbose_limit. In both cases, a value of zero removes&lt;br /&gt;
    the logging limit. Once the limit is reached, logging can be &lt;br /&gt;
    re-enabled by clearing the logging counter or the packet counter for&lt;br /&gt;
    that rule. See the ipfw reset log command.&lt;br /&gt;
    Note: logging is done after all other packet matching conditions &lt;br /&gt;
    have been successfully verified and before performing the final &lt;br /&gt;
    action accept, deny) on the packet. It’s up to you to decide which&lt;br /&gt;
    rules you want to enable logging on. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION&lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match the packet. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    udp | tcp | icmp  &lt;br /&gt;
        or any protocol names found in /etc/protocols are recognized &lt;br /&gt;
        and may be used. The value specified is the protocol to be&lt;br /&gt;
        matched against. This is a mandatory requirement. &lt;br /&gt;
&lt;br /&gt;
    from src to dst &lt;br /&gt;
        The from and to keywords are used to match against IP&lt;br /&gt;
        addresses. Rules must specify BOTH source and destination&lt;br /&gt;
        parameters. &lt;br /&gt;
        'any' is a special keyword that matches any IP address. &lt;br /&gt;
        'me' is a special keyword that matches any IP address&lt;br /&gt;
        configured on an interface in your FBSD system to represent the&lt;br /&gt;
        PC the firewall is running on. (IE: this box) &lt;br /&gt;
&lt;br /&gt;
        As in 'from me to any' or from 'any to me' or &lt;br /&gt;
        'from 0.0.0.0/0 to any' or from 'any to 0.0.0.0/0' or &lt;br /&gt;
        'from 0.0.0.0 to any' or 'from any to 0.0.0.0' or from &lt;br /&gt;
        'me to 0.0.0.0'  IP addresses are specified as a dotted IP&lt;br /&gt;
        address numeric form/mask-length or as single dotted IP address&lt;br /&gt;
        numeric form. &lt;br /&gt;
        This is a mandatory requirement. See this link for &lt;br /&gt;
        help on writing mask-lengths. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
    port number &lt;br /&gt;
        For protocols which support port numbers (such as TCP and UDP).&lt;br /&gt;
        It’s mandatory that you code the port number of the service &lt;br /&gt;
        you want to match on. Service names (from /etc/services) may be &lt;br /&gt;
        used instead of numeric port values. &lt;br /&gt;
&lt;br /&gt;
    in | out &lt;br /&gt;
        Matches incoming or outgoing packets, respectively. in and out&lt;br /&gt;
        are keywords and it’s mandatory that you code one or the other &lt;br /&gt;
        as part of your rule matching criterion.&lt;br /&gt;
&lt;br /&gt;
    via IFN &lt;br /&gt;
        Matches packets going through the interface specified by exact &lt;br /&gt;
        name. IFN = interface-name. The via keyword causes the interface&lt;br /&gt;
        to always be checked as part of the match process. &lt;br /&gt;
        via is mandatory.&lt;br /&gt;
&lt;br /&gt;
    setup &lt;br /&gt;
        This is a mandatory keyword that identifies the session start&lt;br /&gt;
        request for TCP packets.&lt;br /&gt;
&lt;br /&gt;
    keep-state &lt;br /&gt;
        This is a mandatory keyword. Upon a match, the firewall will&lt;br /&gt;
        create a dynamic rule whose default behavior is to match &lt;br /&gt;
        bidirectional traffic between source and destination IP/port&lt;br /&gt;
        using the same protocol. &lt;br /&gt;
&lt;br /&gt;
    limit {src-addr | src-port | dst-addr | dst-port}&lt;br /&gt;
        The firewall will only allow N connections with the same set of&lt;br /&gt;
        parameters as specified in the rule. One or more of source &lt;br /&gt;
        and destination addresses and ports can be specified. &lt;br /&gt;
        The ‘limit’ and 'keep-state’ cannot be used on same rule. &lt;br /&gt;
        Limit provides the same stateful function as ‘keep-state’ &lt;br /&gt;
        plus its own functions.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Option ==&lt;br /&gt;
&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors. This interrogation ability works for all the protocols. &lt;br /&gt;
&lt;br /&gt;
The 'check-state' &amp;lt;action&amp;gt; is used to identify where in the IPFW rules set the packet is to be tested against the dynamic rules facility. On a match the packet exits the firewall to continue on its way and a new rule is dynamic created for the next anticipated packet being exchanged during this bi-directional session conversation. On a no match the packet advances to the next rule in the rule set for testing.&lt;br /&gt;
&lt;br /&gt;
The dynamic rules facility is vulnerable to resource depletion from a SYN-flood attack which would open a huge number of dynamic rules. To counter this attack, FBSD version 4.5 added another new option named limit. This option is used to limit the number of simultaneous session conversations by interrogating the rule's source or destinations fields as directed by the limit option and using the packet's IP address found there. In a search of the open dynamic rules counting the number of times this rule and IP address combination occurred, if this count is greater that the value specified on the limit option, the packet is discarded.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Logging Firewall Messages ==&lt;br /&gt;
The benefits of logging are obvious, provides information like, what packets have been dropped, what addresses they came from, and where they were going. This gives you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPFW will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log verb to those rules. Normally only deny rules are logged, like the deny rule for incoming icmp pings. It's very customary to duplicate the ipfw default deny everything rule with the log verb included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
Logging is a two edged sword. If you're not careful, you can lose yourself in the over abundance of log data and fill all your free disk space with growing log files. DoS attacks that fill up disk drives is one of the oldest attacks around. These log messages are not only written to syslogd, but also are displayed on the root console screen and soon become very annoying.&lt;br /&gt;
&lt;br /&gt;
The IPFIREWALL_VERBOSE_LIMIT=5 kernel option limits the number of consecutive messages sent to the system logger syslogd concerning the packet matching of a given rule. When this option is enabled in the kernel, the number of consecutive messages concerning a particular rule is capped at the number specified. There is nothing to be gained from 200 log messages saying the same identical thing. For instance, 5 consecutive messages concerning a particular rule would be logged to syslogd, the remainder identical consecutive messages would be counted and posted to the syslogd with a phrase like this:&lt;br /&gt;
&lt;br /&gt;
last message repeated 45 times&lt;br /&gt;
&lt;br /&gt;
All logged packet messages are written by default to /var/log/security file, which is defined in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Most experienced IPFW users create a file containing the rules and code them in a manner compatible with running them as a script. The major benefit of doing this is the firewall rules can be refreshed in mass with out the need of rebooting the system to activate the new rules. This method is very convenient in testing new rules as the procedure can be executed as many times as needed. Being a script, you can use symbolic substitution to code frequent used values and substituting them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the 'sh', 'csh', 'tcsh' shells.&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
The value to populate the symbolic field must be enclosed in &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
############### start of example ipfw rules script ############# &lt;br /&gt;
# &lt;br /&gt;
ipfw –q -f flush     # Delete all rules&lt;br /&gt;
# Set defaults &lt;br /&gt;
oif=&amp;quot;tun0&amp;quot; # out interface &lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;      # ISP's dns server IP address&lt;br /&gt;
cmd=&amp;quot;ipfw -q add &amp;quot;     # build rule prefix&lt;br /&gt;
ks=&amp;quot;keep-state&amp;quot;        # just too lazy to key this each time&lt;br /&gt;
$cmd 00500 check-state&lt;br /&gt;
$cmd 00502 deny all from any to any frag&lt;br /&gt;
$cmd 00501 deny tcp from any to any established&lt;br /&gt;
$cmd 00600 allow tcp from any to any 80 out via $oif setup $ks&lt;br /&gt;
$cmd 00610 allow tcp from any to $odns 53 out via $oif setup $ks &lt;br /&gt;
$cmd 00611 allow udp from any to $odns 53 out via $oif $ks&lt;br /&gt;
################### End of example ipfw rules script ############&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's all there is to it. The rules are not important in this example; how the symbolic substitution field are populated and used are. &lt;br /&gt;
&lt;br /&gt;
If the above example was in /etc/ipfw.rules file, I could reload these rules by entering on the FBSD command &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sh /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
The /etc/ipfw.rules file could be located anywhere you want and the file could be named anything you wanted.&lt;br /&gt;
&lt;br /&gt;
The same thing could also to accomplished doing it this way as a text file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
ipfw -q add check-state&lt;br /&gt;
ipfw -q add deny all from any to any frag&lt;br /&gt;
ipfw -q add deny tcp from any to any established&lt;br /&gt;
ipfw -q add allow tcp from any to any 80 out via tun0 setup keep-state&lt;br /&gt;
ipfw -q add allow tcp from any to 192.0.2.11 53 out via tun0 setup keep-state &lt;br /&gt;
ipfw -q add 00611 allow udp from any to 192.0.2.11 53 out via tun0 keep-state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Set ==&lt;br /&gt;
 &lt;br /&gt;
The following non-NATed rule set is an example of how to code a very secure ‘inclusive’ type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication with in the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NICs are cabled to private LANs (local area networks) behind the firewall, those interfaces must have rules coded to allow free unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections: all the free unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘allow’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the proto, port, in/out, via and keep state options coded. The ‘proto tcp’ rules have the ‘setup’ option included to identify the start session request as the trigger packet to be posted to the keep state stateful table. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First is these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. The second reason is that by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; the packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php  and do a port number lookup to find what the purpose of that port number is. Check out this link for port numbers used for Trojans: http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Example Inclusive Rule Set ==&lt;br /&gt;
&lt;br /&gt;
The following non-NATed rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for you own. Just comment out any pass rules for services you don’t want. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing, just add a deny rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
You will see the pattern in the usage of these rules.&lt;br /&gt;
&lt;br /&gt;
All statements that are a request to start a session to the public Internet use keep-state.&lt;br /&gt;
&lt;br /&gt;
All the authorized services that originate from the public Internet have the limit option to stop flooding.&lt;br /&gt;
&lt;br /&gt;
All rules use in or out to clarify direction.&lt;br /&gt;
&lt;br /&gt;
All rules use via interface name to specify the interface the packet is traveling over.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################ Start of IPFW rules file ###############################&lt;br /&gt;
# Flush out the list before we begin.&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
&lt;br /&gt;
# Set rules command prefix&lt;br /&gt;
cmd=&amp;quot;ipfw -q add&amp;quot;&lt;br /&gt;
pif=&amp;quot;dc0&amp;quot;     # public interface name of Nic card &lt;br /&gt;
                        # facing the public Internet&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside Lan Interface for private network&lt;br /&gt;
# Not needed unless you have Lan. &lt;br /&gt;
# Change xl0 to your Lan Nic card interface name&lt;br /&gt;
################################################################# &lt;br /&gt;
#$cmd 00005 allow all from any to any via xl0 &lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
$cmd 00010 allow all from any to any via lo0 &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Allow the packet through if it has previous been added to the &lt;br /&gt;
# the &amp;quot;dynamic&amp;quot; rules table by an allow keep-state statement. &lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 00015 check-state&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network or from this gateway server&lt;br /&gt;
# destine for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# x.x.x.x must be the IP address of your ISP’s DNS&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
$cmd 00110 allow tcp from any to x.x.x.x 53 out via $pif setup keep-state &lt;br /&gt;
$cmd 00111 allow udp from any to x.x.x.x 53 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable/DSL configurations.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ connection to the public Internet.&lt;br /&gt;
# so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
$cmd 00120 allow log udp from any to any 67 out via $pif keep-state &lt;br /&gt;
#$cmd 00120 allow udp from any to x.x.x.x 67 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
$cmd 00200 allow tcp from any to any 80 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
$cmd 00220 allow tcp from any to any 443 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
$cmd 00230 allow tcp from any to any 25 out via $pif setup keep-state&lt;br /&gt;
$cmd 00231 allow tcp from any to any 110 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD (make install &amp;amp; CVSUP) functions&lt;br /&gt;
# Basically give user root &amp;quot;GOD&amp;quot; privileges.&lt;br /&gt;
$cmd 00240 allow tcp from me to any out via $pif setup keep-state uid root&lt;br /&gt;
&lt;br /&gt;
# Allow out ping &lt;br /&gt;
$cmd 00250 allow icmp from any to any out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
$cmd 00260 allow tcp from any to any 37 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news (IE: news groups)&lt;br /&gt;
$cmd 00270 allow tcp from any to any 119 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
$cmd 00280 allow tcp from any to any 22 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois &lt;br /&gt;
$cmd 00290 allow tcp from any to any 43 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# deny and log everything else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
$cmd 00299 deny log all from any to any out via $pif&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destine for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Deny all inbound traffic from non-routable reserved address spaces&lt;br /&gt;
$cmd 00300 deny all from 192.168.0.0/16 to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 00301 deny all from 172.16.0.0/12 to anyin via $pif    #RFC 1918 private IP&lt;br /&gt;
$cmd 00302 deny all from 10.0.0.0/8 to anyin via $pif       #RFC 1918 private IP&lt;br /&gt;
$cmd 00303 deny all from 127.0.0.0/8 to anyin via $pif      #loopback&lt;br /&gt;
$cmd 00304 deny all from 0.0.0.0/8 to anyin via $pif            #loopback&lt;br /&gt;
$cmd 00305 deny all from 169.254.0.0/16 to anyin via $pif   #DHCP auto-config&lt;br /&gt;
$cmd 00306 deny all from 192.0.2.0/24 to anyin via $pif     #reserved for doc's&lt;br /&gt;
$cmd 00307 deny all from 204.152.64.0/23 to anyin via $pif  #Sun cluster interconnect&lt;br /&gt;
$cmd 00308 deny all from 224.0.0.0/3 to anyin via $pif      #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
# Deny public pings&lt;br /&gt;
$cmd 00310 deny icmp from any to anyin via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny ident&lt;br /&gt;
$cmd 00315 deny tcp from any to any 113in via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
$cmd 00320 deny tcp from any to any 137in via $pif &lt;br /&gt;
$cmd 00321 deny tcp from any to any 138in via $pif &lt;br /&gt;
$cmd 00322 deny tcp from any to any 139in via $pif &lt;br /&gt;
$cmd 00323 deny tcp from any to any 81 in via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny any late arriving packets &lt;br /&gt;
$cmd 00330 deny all from any to any frag in via $pif&lt;br /&gt;
&lt;br /&gt;
# Deny ACK packets that did not match the dynamic rule table&lt;br /&gt;
$cmd 00332 deny tcp from any to any established in via $pif&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. &lt;br /&gt;
# Only necessary for cable or DSL configurations. &lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to &lt;br /&gt;
# the public Internet. This is the same IP address you captured &lt;br /&gt;
# and used in the outbound section.&lt;br /&gt;
#$cmd 00360 allow udp from any to x.x.x.x 67 in via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
$cmd 00400 allow tcp from any to me 80 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
$cmd 00410 allow tcp from any to me 22 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID &amp;amp; PW are passed over public&lt;br /&gt;
# Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
$cmd 00420 allow tcp from any to me 23 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Reject &amp;amp; Log all incoming connections from the outside&lt;br /&gt;
$cmd 00499 deny log all from any to any in via $pif&lt;br /&gt;
&lt;br /&gt;
# Everything else is denied by default &lt;br /&gt;
# deny and log all packets that fell through to see what they are&lt;br /&gt;
$cmd 00999 deny log all from any to any&lt;br /&gt;
################ End of IPFW rules file ###############################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stateful + NATD Rule Set ==&lt;br /&gt;
&lt;br /&gt;
There are some additional configuration statements that need to be enabled to activate the NAT function of IPFW. The kernel source needs an 'option divert' statement added to the other IPFIREWALL statements compiled into a custom kernel. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
option   IPFIREWALL                  # Adds filtering code into kernel&lt;br /&gt;
option   IPFIREWALL_VERBOSE          # enable logging thru syslogd(8)&lt;br /&gt;
option   IPFIREWALL_VERBOSE_LIMIT=5  # stop attack via syslog flooding&lt;br /&gt;
option   IPDIVERT                    # needed to use natd from IPFW&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The rc.conf needs the following statements added to the already mentioned statements which are reproduced here: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging &lt;br /&gt;
&lt;br /&gt;
natd_enable=&amp;quot;YES&amp;quot;                   # Enable NATD function &lt;br /&gt;
natd_interface=&amp;quot;rl0&amp;quot;                # interface name of public internet Nic&lt;br /&gt;
natd_flags=&amp;quot;-dynamic -m&amp;quot;            # -m = preserve port numbers if possible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Utilizing stateful rules with divert natd rules (network address translation) greatly complicates the rule set coding logic. The positioning of the check-state, and 'divert natd' rules in the rule set becomes very, very critical. This is no longer a simple fall-through logic flow. A new action type is used, called 'skipto'. To use the skipto command it is mandatory that you number each rule so you know exactly where the skipto rule number is you are really jumping to.&lt;br /&gt;
&lt;br /&gt;
The following is an uncommented example of one coding method, selected here to explain the sequence of the packet flow through the rule sets.&lt;br /&gt;
&lt;br /&gt;
The processing flow starts with the first rule from the top of the rule file and progress one rule at a time deeper into the file until the end is reach or the packet being tested to the selection criteria matches and the packet is released out of the firewall. It's important to take notice of the location of rule numbers 100 101, 450, 500, and 510. These rules control the translation of the outbound and inbound packets so their entries in the keep-state dynamic table always register the private Lan IP address. Next, notice that all the allow and deny rules specify the direction the packet is going (IE outbound or inbound) and the interface. Also notice that all the start outbound session requests all skipto rule 500 for the network address translation. &lt;br /&gt;
&lt;br /&gt;
Lets say a LAN user uses their web browser to get a web page. Web pages use port 80 to communicate over. So when the packet enters the firewall, it does not match rule 100 because it's headed out not in. It passes rule 101 because this is the first packet so it has not been posted to the keep-state dynamic table yet. The packet finally comes to rule 125 and matches. It's outbound through the NIC facing the public Internet. The packet still has its source IP address as a private LAN IP address. On the match to this rule, two actions take place. The keep-state option will post this rule into the keep-state dynamic rules table and the specified action is executed. The action is part of the info posted to the dynamic table. In this case it's &amp;quot;skipto rule 500&amp;quot;. Rule 500 NATs the packet IP address and out it goes. Remember this, this is very important. This packet makes its way to the destination and returns and enters the top of the rule set. This time it does match rule 100 and has its destination IP address mapped back to its corresponding LAN IP address. It then is processed by the check-state rule, it's found in the table as an existing session conversation and is released to the LAN. It goes to the LAN PC that sent it and a new packet is sent requesting another segment of the data from the remote server. This time it gets checked by the check-state rule and its outbound entry is found. The associated action skipto 500 is executed. The packet jumps to rule 500, gets NATed and is released to exit out the external NIC.&lt;br /&gt;
&lt;br /&gt;
On the inbound side, everything coming in that is part of an existing session conversation is being automatically handled by the check-state rule and the properly placed divert natd rules. All we have to address is denying all the bad packets and only allowing in the authorized services. Lets say there is an apache server running on the firewall box and we want people on the public Internet to be able to access the local web site. The new inbound start request packet matches rule 100 and its IP address is mapped to the LAN IP address for the firewall box. The packet is then matched against all the nasty things we want to check for and finally matches against rule 420. On a match two things occur, the limit option is an extension to keep-state. The packet rule is posted to the keep-state dynamic table, but this time any new session requests originating from that same source IP address is limited to two. This defends against DoS attacks of services running on the specified port number. The action is allow so the packet is released to the LAN. On return the check-state rule recognizes the packet as belonging to an existing session conversation, sends it to rule 500 for NATing, and is released to the outbound interface.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
cmd=&amp;quot;ipfw -q add&amp;quot;&lt;br /&gt;
skip=&amp;quot;skipto 500&amp;quot;&lt;br /&gt;
pif=rl0&lt;br /&gt;
ks=&amp;quot;keep-state&amp;quot;&lt;br /&gt;
good_tcpo=&amp;quot;22,25,37,43,53,80,443,110,119&amp;quot;&lt;br /&gt;
&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
&lt;br /&gt;
$cmd 002 allow all from any to any via xl0  # exclude Lan traffic&lt;br /&gt;
$cmd 003 allow all from any to any via lo0  # exclude loopback traffic&lt;br /&gt;
&lt;br /&gt;
$cmd 100 divert natd ip from any to any in via $pif&lt;br /&gt;
$cmd 101 check-state&lt;br /&gt;
&lt;br /&gt;
# Authorized outbound packets&lt;br /&gt;
$cmd 120 $skip tcp from any to xx.168.240.2 53 out via $pif setup $ks&lt;br /&gt;
$cmd 121 $skip udp from any to xx.168.240.5 53 out via $pif $ks&lt;br /&gt;
$cmd 125 $skip tcp from any to any $good_tcpo  out via $pif setup $ks&lt;br /&gt;
$cmd 130 $skip icmp from any to any                 out via $pif $ks&lt;br /&gt;
$cmd 135 $skip udp from any to any 123             out via $pif $ks&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Deny all inbound traffic from non-routable reserved address spaces&lt;br /&gt;
$cmd 300 deny all from 192.168.0.0/16  to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 301 deny all from 172.16.0.0/12    to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 302 deny all from 10.0.0.0/8         to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 303 deny all from 127.0.0.0/8       to any in via $pif  #loopback&lt;br /&gt;
$cmd 304 deny all from 0.0.0.0/8           to any in via $pif  #loopback&lt;br /&gt;
$cmd 305 deny all from 169.254.0.0/16  to any in via $pif  #DHCP auto-config&lt;br /&gt;
$cmd 306 deny all from 192.0.2.0/24      to any in via $pif  #reserved for doc's&lt;br /&gt;
$cmd 307 deny all from 204.152.64.0/23 to any in via $pif  #Sun cluster &lt;br /&gt;
$cmd 308 deny all from 224.0.0.0/3        to any in via $pif  #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
# Authorized inbound packets&lt;br /&gt;
$cmd 400 allow udp from xx.70.207.54 to any 68 in $ks&lt;br /&gt;
$cmd 420 allow tcp from any to me 80 in via $pif setup limit src-addr 1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$cmd 450 deny log ip from any to any&lt;br /&gt;
&lt;br /&gt;
# This is skipto location for outbound stateful rules&lt;br /&gt;
$cmd 500 divert natd ip from any to any out via $pif&lt;br /&gt;
$cmd 510 allow ip from any to any&lt;br /&gt;
&lt;br /&gt;
######################## end of rules  ##################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following is pretty much the same as above, but uses a self documenting coding style full of description comments to help the inexperienced IPFW rule writer to better understand what the rules are doing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
&lt;br /&gt;
################ Start of IPFW rules file ###############################&lt;br /&gt;
# Flush out the list before we begin.&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
&lt;br /&gt;
# Set rules command prefix&lt;br /&gt;
cmd=&amp;quot;ipfw -q add&amp;quot;&lt;br /&gt;
skip=&amp;quot;skipto 800&amp;quot;&lt;br /&gt;
pif=&amp;quot;rl0&amp;quot;     # public interface name of Nic card&lt;br /&gt;
              # facing the public internet&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# No restrictions on Inside Lan Interface for private network&lt;br /&gt;
# Change xl0 to your Lan Nic card interface name&lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 005 allow all from any to any via xl0&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# No restrictions on Loopback Interface&lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 010 allow all from any to any via lo0&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# check if packet is inbound and nat address if it is&lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 014 divert natd ip from any to any in via $pif&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Allow the packet through if it has previous been added to the&lt;br /&gt;
# the &amp;quot;dynamic&amp;quot; rules table by a allow keep-state statement.&lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 015 check-state&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public internet (Outbound Section)&lt;br /&gt;
# Interrogate session start requests originating from behind the&lt;br /&gt;
# firewall on the private network or from this gateway server&lt;br /&gt;
# destine for the public internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# x.x.x.x must be the IP address of your ISP's DNS&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
$cmd 020 $skip tcp from any to x.x.x.x 53 out via $pif setup keep-state&lt;br /&gt;
$cmd 021 $skip udp from any to x.x.x.x 53 out via $pif  keep-state&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable/DSL configurations.&lt;br /&gt;
$cmd 030 $skip udp from any to x.x.x.x 67 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
$cmd 040 $skip tcp from any to any 80 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
$cmd 050 $skip tcp from any to any 443 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
$cmd 060 $skip tcp from any to any 25   out via $pif setup keep-state&lt;br /&gt;
$cmd 061 $skip tcp from any to any 110 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD (make install &amp;amp; CVSUP) functions&lt;br /&gt;
# Basically give user root &amp;quot;GOD&amp;quot; privileges.&lt;br /&gt;
$cmd 070 $skip tcp from me to any out via $pif setup keep-state uid root&lt;br /&gt;
&lt;br /&gt;
# Allow out ping&lt;br /&gt;
$cmd 080 $skip icmp from any to any out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
$cmd 090 $skip tcp from any to any 37 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news (IE: news groups)&lt;br /&gt;
$cmd 100 $skip tcp from any to any 119 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
$cmd 110 $skip tcp from any to any 22 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois&lt;br /&gt;
$cmd 120 $skip tcp from any to any 43 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow ntp time server&lt;br /&gt;
$cmd 130 $skip udp from any to any 123 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public internet&lt;br /&gt;
# destine for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Deny all inbound traffic from non-routable reserved address spaces&lt;br /&gt;
$cmd 300 deny all from 192.168.0.0/16  to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 301 deny all from 172.16.0.0/12    to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 302 deny all from 10.0.0.0/8         to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 303 deny all from 127.0.0.0/8       to any in via $pif  #loopback&lt;br /&gt;
$cmd 304 deny all from 0.0.0.0/8           to any in via $pif  #loopback&lt;br /&gt;
$cmd 305 deny all from 169.254.0.0/16  to any in via $pif  #DHCP auto-config&lt;br /&gt;
$cmd 306 deny all from 192.0.2.0/24      to any in via $pif  #reserved for doc's&lt;br /&gt;
$cmd 307 deny all from 204.152.64.0/23 to any in via $pif  #Sun cluster &lt;br /&gt;
$cmd 308 deny all from 224.0.0.0/3        to any in via $pif  #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
# Deny ident&lt;br /&gt;
$cmd 315 deny tcp from any to any 113 in via $pif&lt;br /&gt;
&lt;br /&gt;
# Deny all Netbios service. 137=name, 138=datagram, 139=session&lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
$cmd 320 deny tcp from any to any 137 in via $pif&lt;br /&gt;
$cmd 321 deny tcp from any to any 138 in via $pif&lt;br /&gt;
$cmd 322 deny tcp from any to any 139 in via $pif&lt;br /&gt;
$cmd 323 deny tcp from any to any 81   in via $pif&lt;br /&gt;
&lt;br /&gt;
# Deny any late arriving packets&lt;br /&gt;
$cmd 330 deny all from any to any frag in via $pif&lt;br /&gt;
&lt;br /&gt;
# Deny ACK packets that did not match the dynamic rule table&lt;br /&gt;
$cmd 332 deny tcp from any to any established in via $pif&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP's DHCP server as it's the only&lt;br /&gt;
# authorized source to send this packet type.&lt;br /&gt;
# Only necessary for cable or DSL configurations.&lt;br /&gt;
# This rule is not needed for 'user ppp' type connection to&lt;br /&gt;
# the public internet. This is the same IP address you captured&lt;br /&gt;
# and used in the outbound section.&lt;br /&gt;
$cmd 360 allow udp from x.x.x.x to any 68 in via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
$cmd 370 allow tcp from any to me 80 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
$cmd 380 allow tcp from any to me 22 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID &amp;amp; PW are passed over public&lt;br /&gt;
# internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
$cmd 390 allow tcp from any to me 23 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Reject &amp;amp; Log all unauthorized incoming connections from the public internet&lt;br /&gt;
$cmd 400 deny log all from any to any in via $pif&lt;br /&gt;
&lt;br /&gt;
# Reject &amp;amp; Log all unauthorized out going connections to the public internet&lt;br /&gt;
$cmd 450 deny log all from any to any out via $pif&lt;br /&gt;
&lt;br /&gt;
# This is skipto location for outbound stateful rules&lt;br /&gt;
$cmd 800 divert natd ip from any to any out via $pif&lt;br /&gt;
$cmd 801 allow ip from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Everything else is denied by default&lt;br /&gt;
# deny and log all packets that fell through to see what they are&lt;br /&gt;
$cmd 999 deny log all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
################ End of IPFW rules file ###############################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T22:35:45Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFW Rule Sets ==&lt;br /&gt;
A rule set is a group of ipfw rules coded to allow or deny packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc.) is predefined by its protocol and port number. This is the basic selection criteria used to create rules which will allow or deny services. &lt;br /&gt;
&lt;br /&gt;
When a packet enters the firewall it is compared against the first rule in the rule set and progresses one rule at a time, moving from top to bottom of the set in ascending rule number sequence order. When the packet matches a rule selection parameter, the rule's action field value is executed and the search of the rule set terminates for that packet. This is referred to as the 'first match wins' search method. If the packet does not match any of the rules, it gets caught by the mandatory ipfw default rule, number 65535 which denies all packets and discards them without any reply back to the originating destination. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section of the Installers Guide is based on using rules that contain the stateful ‘keep state’ and ‘limit’ options. This is the basic framework for coding an inclusive type firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destine for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is denied by default design. Inclusive firewalls are much more secure than exclusive firewall rule sets and are the only rule set type covered here in. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to what is necessary to create a standard inclusive type firewall rule set. For a complete rule syntax description see the online ‘man ipfw’ page at &lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/cgi/man.cgi?query=ipfw&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at &lt;br /&gt;
the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Syntax = CMD RULE# ACTION LOGGING SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
CMD Each rule has to be prefixed with the following to add the rule to the internal table,&lt;br /&gt;
&lt;br /&gt;
ipfw add&lt;br /&gt;
&lt;br /&gt;
RULE# Coding rule numbers is not a mandatory requirement. Rule numbers will automatically be assigned when the rules are loaded into the internal IPFW tables. Coding your own rule numbers means the numbers will not change during loading and gives you a fixed rule number which is listed in the log along with other information about the packet being logged. The rule number is how you relate the logged packet back to the rule that caused the packet to be logged. If a rule is entered without a number, ipfw will assign one. &lt;br /&gt;
&lt;br /&gt;
ACTIONS&lt;br /&gt;
&lt;br /&gt;
A rule can be associated with one of the following actions which will be &lt;br /&gt;
executed when the packet matches the selection criterion of the rule.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
allow | accept | pass | permit&lt;br /&gt;
&lt;br /&gt;
       These all mean the same thing which is to allow &lt;br /&gt;
       packets that match the rule to exit the firewall &lt;br /&gt;
       rule processing. The search terminates.&lt;br /&gt;
&lt;br /&gt;
check-state&lt;br /&gt;
&lt;br /&gt;
      Checks the packet against the dynamic rules table. &lt;br /&gt;
      If a match is found, execute the action associated with &lt;br /&gt;
      the rule which generated this dynamic rule, otherwise move &lt;br /&gt;
      to the next rule. The check-state rule does not have &lt;br /&gt;
      selection criteria. If no check-state rule is present in &lt;br /&gt;
      the rule set, the dynamic rules table is checked at the &lt;br /&gt;
      first keep-state or limit rule.&lt;br /&gt;
&lt;br /&gt;
deny | drop&lt;br /&gt;
&lt;br /&gt;
     Both words mean the same thing which is to discard packets &lt;br /&gt;
     that match this rule. The search terminates.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
LOGGING&lt;br /&gt;
&lt;br /&gt;
log or logamount number&lt;br /&gt;
&lt;br /&gt;
    When a packet matches a rule with the log keyword, a message will be&lt;br /&gt;
    logged to syslogd with a facility name of SECURITY. The logging &lt;br /&gt;
    only occurs if the number of packets logged so far for that&lt;br /&gt;
    particular rule does not exceed the logamount parameter. If no&lt;br /&gt;
    logamount is specified, the limit is taken from the sysctl variable&lt;br /&gt;
    net.inet.ip.fw.verbose_limit. In both cases, a value of zero removes&lt;br /&gt;
    the logging limit. Once the limit is reached, logging can be &lt;br /&gt;
    re-enabled by clearing the logging counter or the packet counter for&lt;br /&gt;
    that rule. See the ipfw reset log command.&lt;br /&gt;
    Note: logging is done after all other packet matching conditions &lt;br /&gt;
    have been successfully verified and before performing the final &lt;br /&gt;
    action accept, deny) on the packet. It’s up to you to decide which&lt;br /&gt;
    rules you want to enable logging on. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION&lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match the packet. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    udp | tcp | icmp  &lt;br /&gt;
        or any protocol names found in /etc/protocols are recognized &lt;br /&gt;
        and may be used. The value specified is the protocol to be&lt;br /&gt;
        matched against. This is a mandatory requirement. &lt;br /&gt;
&lt;br /&gt;
    from src to dst &lt;br /&gt;
        The from and to keywords are used to match against IP&lt;br /&gt;
        addresses. Rules must specify BOTH source and destination&lt;br /&gt;
        parameters. &lt;br /&gt;
        'any' is a special keyword that matches any IP address. &lt;br /&gt;
        'me' is a special keyword that matches any IP address&lt;br /&gt;
        configured on an interface in your FBSD system to represent the&lt;br /&gt;
        PC the firewall is running on. (IE: this box) &lt;br /&gt;
&lt;br /&gt;
        As in 'from me to any' or from 'any to me' or &lt;br /&gt;
        'from 0.0.0.0/0 to any' or from 'any to 0.0.0.0/0' or &lt;br /&gt;
        'from 0.0.0.0 to any' or 'from any to 0.0.0.0' or from &lt;br /&gt;
        'me to 0.0.0.0'  IP addresses are specified as a dotted IP&lt;br /&gt;
        address numeric form/mask-length or as single dotted IP address&lt;br /&gt;
        numeric form. &lt;br /&gt;
        This is a mandatory requirement. See this link for &lt;br /&gt;
        help on writing mask-lengths. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
    port number &lt;br /&gt;
        For protocols which support port numbers (such as TCP and UDP).&lt;br /&gt;
        It’s mandatory that you code the port number of the service &lt;br /&gt;
        you want to match on. Service names (from /etc/services) may be &lt;br /&gt;
        used instead of numeric port values. &lt;br /&gt;
&lt;br /&gt;
    in | out &lt;br /&gt;
        Matches incoming or outgoing packets, respectively. in and out&lt;br /&gt;
        are keywords and it’s mandatory that you code one or the other &lt;br /&gt;
        as part of your rule matching criterion.&lt;br /&gt;
&lt;br /&gt;
    via IFN &lt;br /&gt;
        Matches packets going through the interface specified by exact &lt;br /&gt;
        name. IFN = interface-name. The via keyword causes the interface&lt;br /&gt;
        to always be checked as part of the match process. &lt;br /&gt;
        via is mandatory.&lt;br /&gt;
&lt;br /&gt;
    setup &lt;br /&gt;
        This is a mandatory keyword that identifies the session start&lt;br /&gt;
        request for TCP packets.&lt;br /&gt;
&lt;br /&gt;
    keep-state &lt;br /&gt;
        This is a mandatory keyword. Upon a match, the firewall will&lt;br /&gt;
        create a dynamic rule whose default behavior is to match &lt;br /&gt;
        bidirectional traffic between source and destination IP/port&lt;br /&gt;
        using the same protocol. &lt;br /&gt;
&lt;br /&gt;
    limit {src-addr | src-port | dst-addr | dst-port}&lt;br /&gt;
        The firewall will only allow N connections with the same set of&lt;br /&gt;
        parameters as specified in the rule. One or more of source &lt;br /&gt;
        and destination addresses and ports can be specified. &lt;br /&gt;
        The ‘limit’ and 'keep-state’ cannot be used on same rule. &lt;br /&gt;
        Limit provides the same stateful function as ‘keep-state’ &lt;br /&gt;
        plus its own functions.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Option ==&lt;br /&gt;
&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors. This interrogation ability works for all the protocols. &lt;br /&gt;
&lt;br /&gt;
The 'check-state' &amp;lt;action&amp;gt; is used to identify where in the IPFW rules set the packet is to be tested against the dynamic rules facility. On a match the packet exits the firewall to continue on its way and a new rule is dynamic created for the next anticipated packet being exchanged during this bi-directional session conversation. On a no match the packet advances to the next rule in the rule set for testing.&lt;br /&gt;
&lt;br /&gt;
The dynamic rules facility is vulnerable to resource depletion from a SYN-flood attack which would open a huge number of dynamic rules. To counter this attack, FBSD version 4.5 added another new option named limit. This option is used to limit the number of simultaneous session conversations by interrogating the rule's source or destinations fields as directed by the limit option and using the packet's IP address found there. In a search of the open dynamic rules counting the number of times this rule and IP address combination occurred, if this count is greater that the value specified on the limit option, the packet is discarded.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Logging Firewall Messages ==&lt;br /&gt;
The benefits of logging are obvious, provides information like, what packets have been dropped, what addresses they came from, and where they were going. This gives you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPFW will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log verb to those rules. Normally only deny rules are logged, like the deny rule for incoming icmp pings. It's very customary to duplicate the ipfw default deny everything rule with the log verb included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
Logging is a two edged sword. If you're not careful, you can lose yourself in the over abundance of log data and fill all your free disk space with growing log files. DoS attacks that fill up disk drives is one of the oldest attacks around. These log messages are not only written to syslogd, but also are displayed on the root console screen and soon become very annoying.&lt;br /&gt;
&lt;br /&gt;
The IPFIREWALL_VERBOSE_LIMIT=5 kernel option limits the number of consecutive messages sent to the system logger syslogd concerning the packet matching of a given rule. When this option is enabled in the kernel, the number of consecutive messages concerning a particular rule is capped at the number specified. There is nothing to be gained from 200 log messages saying the same identical thing. For instance, 5 consecutive messages concerning a particular rule would be logged to syslogd, the remainder identical consecutive messages would be counted and posted to the syslogd with a phrase like this:&lt;br /&gt;
&lt;br /&gt;
last message repeated 45 times&lt;br /&gt;
&lt;br /&gt;
All logged packet messages are written by default to /var/log/security file, which is defined in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Most experienced IPFW users create a file containing the rules and code them in a manner compatible with running them as a script. The major benefit of doing this is the firewall rules can be refreshed in mass with out the need of rebooting the system to activate the new rules. This method is very convenient in testing new rules as the procedure can be executed as many times as needed. Being a script, you can use symbolic substitution to code frequent used values and substituting them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the 'sh', 'csh', 'tcsh' shells.&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
The value to populate the symbolic field must be enclosed in &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
############### start of example ipfw rules script ############# &lt;br /&gt;
# &lt;br /&gt;
ipfw –q -f flush     # Delete all rules&lt;br /&gt;
# Set defaults &lt;br /&gt;
oif=&amp;quot;tun0&amp;quot; # out interface &lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;      # ISP's dns server IP address&lt;br /&gt;
cmd=&amp;quot;ipfw -q add &amp;quot;     # build rule prefix&lt;br /&gt;
ks=&amp;quot;keep-state&amp;quot;        # just too lazy to key this each time&lt;br /&gt;
$cmd 00500 check-state&lt;br /&gt;
$cmd 00502 deny all from any to any frag&lt;br /&gt;
$cmd 00501 deny tcp from any to any established&lt;br /&gt;
$cmd 00600 allow tcp from any to any 80 out via $oif setup $ks&lt;br /&gt;
$cmd 00610 allow tcp from any to $odns 53 out via $oif setup $ks &lt;br /&gt;
$cmd 00611 allow udp from any to $odns 53 out via $oif $ks&lt;br /&gt;
################### End of example ipfw rules script ############&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's all there is to it. The rules are not important in this example; how the symbolic substitution field are populated and used are. &lt;br /&gt;
&lt;br /&gt;
If the above example was in /etc/ipfw.rules file, I could reload these rules by entering on the FBSD command &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sh /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
The /etc/ipfw.rules file could be located anywhere you want and the file could be named anything you wanted.&lt;br /&gt;
&lt;br /&gt;
The same thing could also to accomplished doing it this way as a text file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
ipfw -q add check-state&lt;br /&gt;
ipfw -q add deny all from any to any frag&lt;br /&gt;
ipfw -q add deny tcp from any to any established&lt;br /&gt;
ipfw -q add allow tcp from any to any 80 out via tun0 setup keep-state&lt;br /&gt;
ipfw -q add allow tcp from any to 192.0.2.11 53 out via tun0 setup keep-state &lt;br /&gt;
ipfw -q add 00611 allow udp from any to 192.0.2.11 53 out via tun0 keep-state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Set ==&lt;br /&gt;
 &lt;br /&gt;
The following non-NATed rule set is an example of how to code a very secure ‘inclusive’ type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication with in the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NICs are cabled to private LANs (local area networks) behind the firewall, those interfaces must have rules coded to allow free unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections: all the free unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘allow’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the proto, port, in/out, via and keep state options coded. The ‘proto tcp’ rules have the ‘setup’ option included to identify the start session request as the trigger packet to be posted to the keep state stateful table. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First is these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. The second reason is that by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; the packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php  and do a port number lookup to find what the purpose of that port number is. Check out this link for port numbers used for Trojans: http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Example Inclusive Rule Set ==&lt;br /&gt;
&lt;br /&gt;
The following non-NATed rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for you own. Just comment out any pass rules for services you don’t want. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing, just add a deny rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
You will see the pattern in the usage of these rules.&lt;br /&gt;
&lt;br /&gt;
All statements that are a request to start a session to the public Internet use keep-state.&lt;br /&gt;
&lt;br /&gt;
All the authorized services that originate from the public Internet have the limit option to stop flooding.&lt;br /&gt;
&lt;br /&gt;
All rules use in or out to clarify direction.&lt;br /&gt;
&lt;br /&gt;
All rules use via interface name to specify the interface the packet is traveling over.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################ Start of IPFW rules file ###############################&lt;br /&gt;
# Flush out the list before we begin.&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
&lt;br /&gt;
# Set rules command prefix&lt;br /&gt;
cmd=&amp;quot;ipfw -q add&amp;quot;&lt;br /&gt;
pif=&amp;quot;dc0&amp;quot;     # public interface name of Nic card &lt;br /&gt;
                        # facing the public Internet&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside Lan Interface for private network&lt;br /&gt;
# Not needed unless you have Lan. &lt;br /&gt;
# Change xl0 to your Lan Nic card interface name&lt;br /&gt;
################################################################# &lt;br /&gt;
#$cmd 00005 allow all from any to any via xl0 &lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
$cmd 00010 allow all from any to any via lo0 &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Allow the packet through if it has previous been added to the &lt;br /&gt;
# the &amp;quot;dynamic&amp;quot; rules table by an allow keep-state statement. &lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 00015 check-state&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network or from this gateway server&lt;br /&gt;
# destine for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# x.x.x.x must be the IP address of your ISP’s DNS&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
$cmd 00110 allow tcp from any to x.x.x.x 53 out via $pif setup keep-state &lt;br /&gt;
$cmd 00111 allow udp from any to x.x.x.x 53 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable/DSL configurations.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ connection to the public Internet.&lt;br /&gt;
# so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
$cmd 00120 allow log udp from any to any 67 out via $pif keep-state &lt;br /&gt;
#$cmd 00120 allow udp from any to x.x.x.x 67 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
$cmd 00200 allow tcp from any to any 80 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
$cmd 00220 allow tcp from any to any 443 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
$cmd 00230 allow tcp from any to any 25 out via $pif setup keep-state&lt;br /&gt;
$cmd 00231 allow tcp from any to any 110 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD (make install &amp;amp; CVSUP) functions&lt;br /&gt;
# Basically give user root &amp;quot;GOD&amp;quot; privileges.&lt;br /&gt;
$cmd 00240 allow tcp from me to any out via $pif setup keep-state uid root&lt;br /&gt;
&lt;br /&gt;
# Allow out ping &lt;br /&gt;
$cmd 00250 allow icmp from any to any out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
$cmd 00260 allow tcp from any to any 37 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news (IE: news groups)&lt;br /&gt;
$cmd 00270 allow tcp from any to any 119 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
$cmd 00280 allow tcp from any to any 22 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois &lt;br /&gt;
$cmd 00290 allow tcp from any to any 43 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# deny and log everything else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
$cmd 00299 deny log all from any to any out via $pif&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destine for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Deny all inbound traffic from non-routable reserved address spaces&lt;br /&gt;
$cmd 00300 deny all from 192.168.0.0/16 to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 00301 deny all from 172.16.0.0/12 to anyin via $pif    #RFC 1918 private IP&lt;br /&gt;
$cmd 00302 deny all from 10.0.0.0/8 to anyin via $pif       #RFC 1918 private IP&lt;br /&gt;
$cmd 00303 deny all from 127.0.0.0/8 to anyin via $pif      #loopback&lt;br /&gt;
$cmd 00304 deny all from 0.0.0.0/8 to anyin via $pif            #loopback&lt;br /&gt;
$cmd 00305 deny all from 169.254.0.0/16 to anyin via $pif   #DHCP auto-config&lt;br /&gt;
$cmd 00306 deny all from 192.0.2.0/24 to anyin via $pif     #reserved for doc's&lt;br /&gt;
$cmd 00307 deny all from 204.152.64.0/23 to anyin via $pif  #Sun cluster interconnect&lt;br /&gt;
$cmd 00308 deny all from 224.0.0.0/3 to anyin via $pif      #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
# Deny public pings&lt;br /&gt;
$cmd 00310 deny icmp from any to anyin via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny ident&lt;br /&gt;
$cmd 00315 deny tcp from any to any 113in via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
$cmd 00320 deny tcp from any to any 137in via $pif &lt;br /&gt;
$cmd 00321 deny tcp from any to any 138in via $pif &lt;br /&gt;
$cmd 00322 deny tcp from any to any 139in via $pif &lt;br /&gt;
$cmd 00323 deny tcp from any to any 81 in via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny any late arriving packets &lt;br /&gt;
$cmd 00330 deny all from any to any frag in via $pif&lt;br /&gt;
&lt;br /&gt;
# Deny ACK packets that did not match the dynamic rule table&lt;br /&gt;
$cmd 00332 deny tcp from any to any established in via $pif&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. &lt;br /&gt;
# Only necessary for cable or DSL configurations. &lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to &lt;br /&gt;
# the public Internet. This is the same IP address you captured &lt;br /&gt;
# and used in the outbound section.&lt;br /&gt;
#$cmd 00360 allow udp from any to x.x.x.x 67 in via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
$cmd 00400 allow tcp from any to me 80 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
$cmd 00410 allow tcp from any to me 22 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID &amp;amp; PW are passed over public&lt;br /&gt;
# Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
$cmd 00420 allow tcp from any to me 23 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Reject &amp;amp; Log all incoming connections from the outside&lt;br /&gt;
$cmd 00499 deny log all from any to any in via $pif&lt;br /&gt;
&lt;br /&gt;
# Everything else is denied by default &lt;br /&gt;
# deny and log all packets that fell through to see what they are&lt;br /&gt;
$cmd 00999 deny log all from any to any&lt;br /&gt;
################ End of IPFW rules file ###############################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stateful + NATD Rule Set ==&lt;br /&gt;
&lt;br /&gt;
There are some additional configuration statements that need to be enabled to activate the NAT function of IPFW. The kernel source needs an 'option divert' statement added to the other IPFIREWALL statements compiled into a custom kernel. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
option   IPFIREWALL                  # Adds filtering code into kernel&lt;br /&gt;
option   IPFIREWALL_VERBOSE          # enable logging thru syslogd(8)&lt;br /&gt;
option   IPFIREWALL_VERBOSE_LIMIT=5  # stop attack via syslog flooding&lt;br /&gt;
option   IPDIVERT                    # needed to use natd from IPFW&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The rc.conf needs the following statements added to the already mentioned statements which are reproduced here: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging &lt;br /&gt;
&lt;br /&gt;
natd_enable=&amp;quot;YES&amp;quot;                   # Enable NATD function &lt;br /&gt;
natd_interface=&amp;quot;rl0&amp;quot;                # interface name of public internet Nic&lt;br /&gt;
natd_flags=&amp;quot;-dynamic -m&amp;quot;            # -m = preserve port numbers if possible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Utilizing stateful rules with divert natd rules (network address translation) greatly complicates the rule set coding logic. The positioning of the check-state, and 'divert natd' rules in the rule set becomes very, very critical. This is no longer a simple fall-through logic flow. A new action type is used, called 'skipto'. To use the skipto command it is mandatory that you number each rule so you know exactly where the skipto rule number is you are really jumping to.&lt;br /&gt;
&lt;br /&gt;
The following is an uncommented example of one coding method, selected here to explain the sequence of the packet flow through the rule sets.&lt;br /&gt;
&lt;br /&gt;
The processing flow starts with the first rule from the top of the rule file and progress one rule at a time deeper into the file until the end is reach or the packet being tested to the selection criteria matches and the packet is released out of the firewall. It's important to take notice of the location of rule numbers 100 101, 450, 500, and 510. These rules control the translation of the outbound and inbound packets so their entries in the keep-state dynamic table always register the private Lan IP address. Next, notice that all the allow and deny rules specify the direction the packet is going (IE outbound or inbound) and the interface. Also notice that all the start outbound session requests all skipto rule 500 for the network address translation. &lt;br /&gt;
&lt;br /&gt;
Lets say a LAN user uses their web browser to get a web page. Web pages use port 80 to communicate over. So when the packet enters the firewall, it does not match rule 100 because it's headed out not in. It passes rule 101 because this is the first packet so it has not been posted to the keep-state dynamic table yet. The packet finally comes to rule 125 and matches. It's outbound through the NIC facing the public Internet. The packet still has its source IP address as a private LAN IP address. On the match to this rule, two actions take place. The keep-state option will post this rule into the keep-state dynamic rules table and the specified action is executed. The action is part of the info posted to the dynamic table. In this case it's &amp;quot;skipto rule 500&amp;quot;. Rule 500 NATs the packet IP address and out it goes. Remember this, this is very important. This packet makes its way to the destination and returns and enters the top of the rule set. This time it does match rule 100 and has its destination IP address mapped back to its corresponding LAN IP address. It then is processed by the check-state rule, it's found in the table as an existing session conversation and is released to the LAN. It goes to the LAN PC that sent it and a new packet is sent requesting another segment of the data from the remote server. This time it gets checked by the check-state rule and its outbound entry is found. The associated action skipto 500 is executed. The packet jumps to rule 500, gets NATed and is released to exit out the external NIC.&lt;br /&gt;
&lt;br /&gt;
On the inbound side, everything coming in that is part of an existing session conversation is being automatically handled by the check-state rule and the properly placed divert natd rules. All we have to address is denying all the bad packets and only allowing in the authorized services. Lets say there is an apache server running on the firewall box and we want people on the public Internet to be able to access the local web site. The new inbound start request packet matches rule 100 and its IP address is mapped to the LAN IP address for the firewall box. The packet is then matched against all the nasty things we want to check for and finally matches against rule 420. On a match two things occur, the limit option is an extension to keep-state. The packet rule is posted to the keep-state dynamic table, but this time any new session requests originating from that same source IP address is limited to two. This defends against DoS attacks of services running on the specified port number. The action is allow so the packet is released to the LAN. On return the check-state rule recognizes the packet as belonging to an existing session conversation, sends it to rule 500 for NATing, and is released to the outbound interface.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
cmd=&amp;quot;ipfw -q add&amp;quot;&lt;br /&gt;
skip=&amp;quot;skipto 500&amp;quot;&lt;br /&gt;
pif=rl0&lt;br /&gt;
ks=&amp;quot;keep-state&amp;quot;&lt;br /&gt;
good_tcpo=&amp;quot;22,25,37,43,53,80,443,110,119&amp;quot;&lt;br /&gt;
&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
&lt;br /&gt;
$cmd 002 allow all from any to any via xl0  # exclude Lan traffic&lt;br /&gt;
$cmd 003 allow all from any to any via lo0  # exclude loopback traffic&lt;br /&gt;
&lt;br /&gt;
$cmd 100 divert natd ip from any to any in via $pif&lt;br /&gt;
$cmd 101 check-state&lt;br /&gt;
&lt;br /&gt;
# Authorized outbound packets&lt;br /&gt;
$cmd 120 $skip tcp from any to xx.168.240.2 53 out via $pif setup $ks&lt;br /&gt;
$cmd 121 $skip udp from any to xx.168.240.5 53 out via $pif $ks&lt;br /&gt;
$cmd 125 $skip tcp from any to any $good_tcpo  out via $pif setup $ks&lt;br /&gt;
$cmd 130 $skip icmp from any to any                 out via $pif $ks&lt;br /&gt;
$cmd 135 $skip udp from any to any 123             out via $pif $ks&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Deny all inbound traffic from non-routable reserved address spaces&lt;br /&gt;
$cmd 300 deny all from 192.168.0.0/16  to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 301 deny all from 172.16.0.0/12    to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 302 deny all from 10.0.0.0/8         to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 303 deny all from 127.0.0.0/8       to any in via $pif  #loopback&lt;br /&gt;
$cmd 304 deny all from 0.0.0.0/8           to any in via $pif  #loopback&lt;br /&gt;
$cmd 305 deny all from 169.254.0.0/16  to any in via $pif  #DHCP auto-config&lt;br /&gt;
$cmd 306 deny all from 192.0.2.0/24      to any in via $pif  #reserved for doc's&lt;br /&gt;
$cmd 307 deny all from 204.152.64.0/23 to any in via $pif  #Sun cluster &lt;br /&gt;
$cmd 308 deny all from 224.0.0.0/3        to any in via $pif  #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
# Authorized inbound packets&lt;br /&gt;
$cmd 400 allow udp from xx.70.207.54 to any 68 in $ks&lt;br /&gt;
$cmd 420 allow tcp from any to me 80 in via $pif setup limit src-addr 1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$cmd 450 deny log ip from any to any&lt;br /&gt;
&lt;br /&gt;
# This is skipto location for outbound stateful rules&lt;br /&gt;
$cmd 500 divert natd ip from any to any out via $pif&lt;br /&gt;
$cmd 510 allow ip from any to any&lt;br /&gt;
&lt;br /&gt;
######################## end of rules  ##################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following is pretty much the same as above, but uses a self documenting coding style full of description comments to help the inexperienced IPFW rule writer to better understand what the rules are doing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
&lt;br /&gt;
################ Start of IPFW rules file ###############################&lt;br /&gt;
# Flush out the list before we begin.&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
&lt;br /&gt;
# Set rules command prefix&lt;br /&gt;
cmd=&amp;quot;ipfw -q add&amp;quot;&lt;br /&gt;
skip=&amp;quot;skipto 800&amp;quot;&lt;br /&gt;
pif=&amp;quot;rl0&amp;quot;     # public interface name of Nic card&lt;br /&gt;
              # facing the public internet&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# No restrictions on Inside Lan Interface for private network&lt;br /&gt;
# Change xl0 to your Lan Nic card interface name&lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 005 allow all from any to any via xl0&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# No restrictions on Loopback Interface&lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 010 allow all from any to any via lo0&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# check if packet is inbound and nat address if it is&lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 014 divert natd ip from any to any in via $pif&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Allow the packet through if it has previous been added to the&lt;br /&gt;
# the &amp;quot;dynamic&amp;quot; rules table by a allow keep-state statement.&lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 015 check-state&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public internet (Outbound Section)&lt;br /&gt;
# Interrogate session start requests originating from behind the&lt;br /&gt;
# firewall on the private network or from this gateway server&lt;br /&gt;
# destine for the public internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# x.x.x.x must be the IP address of your ISP's DNS&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
$cmd 020 $skip tcp from any to x.x.x.x 53 out via $pif setup keep-state&lt;br /&gt;
$cmd 021 $skip udp from any to x.x.x.x 53 out via $pif  keep-state&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable/DSL configurations.&lt;br /&gt;
$cmd 030 $skip udp from any to x.x.x.x 67 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
$cmd 040 $skip tcp from any to any 80 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
$cmd 050 $skip tcp from any to any 443 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
$cmd 060 $skip tcp from any to any 25   out via $pif setup keep-state&lt;br /&gt;
$cmd 061 $skip tcp from any to any 110 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD (make install &amp;amp; CVSUP) functions&lt;br /&gt;
# Basically give user root &amp;quot;GOD&amp;quot; privileges.&lt;br /&gt;
$cmd 070 $skip tcp from me to any out via $pif setup keep-state uid root&lt;br /&gt;
&lt;br /&gt;
# Allow out ping&lt;br /&gt;
$cmd 080 $skip icmp from any to any out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
$cmd 090 $skip tcp from any to any 37 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news (IE: news groups)&lt;br /&gt;
$cmd 100 $skip tcp from any to any 119 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
$cmd 110 $skip tcp from any to any 22 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois&lt;br /&gt;
$cmd 120 $skip tcp from any to any 43 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow ntp time server&lt;br /&gt;
$cmd 130 $skip udp from any to any 123 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public internet&lt;br /&gt;
# destine for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Deny all inbound traffic from non-routable reserved address spaces&lt;br /&gt;
$cmd 300 deny all from 192.168.0.0/16  to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 301 deny all from 172.16.0.0/12    to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 302 deny all from 10.0.0.0/8         to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 303 deny all from 127.0.0.0/8       to any in via $pif  #loopback&lt;br /&gt;
$cmd 304 deny all from 0.0.0.0/8           to any in via $pif  #loopback&lt;br /&gt;
$cmd 305 deny all from 169.254.0.0/16  to any in via $pif  #DHCP auto-config&lt;br /&gt;
$cmd 306 deny all from 192.0.2.0/24      to any in via $pif  #reserved for doc's&lt;br /&gt;
$cmd 307 deny all from 204.152.64.0/23 to any in via $pif  #Sun cluster &lt;br /&gt;
$cmd 308 deny all from 224.0.0.0/3        to any in via $pif  #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
# Deny ident&lt;br /&gt;
$cmd 315 deny tcp from any to any 113 in via $pif&lt;br /&gt;
&lt;br /&gt;
# Deny all Netbios service. 137=name, 138=datagram, 139=session&lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
$cmd 320 deny tcp from any to any 137 in via $pif&lt;br /&gt;
$cmd 321 deny tcp from any to any 138 in via $pif&lt;br /&gt;
$cmd 322 deny tcp from any to any 139 in via $pif&lt;br /&gt;
$cmd 323 deny tcp from any to any 81   in via $pif&lt;br /&gt;
&lt;br /&gt;
# Deny any late arriving packets&lt;br /&gt;
$cmd 330 deny all from any to any frag in via $pif&lt;br /&gt;
&lt;br /&gt;
# Deny ACK packets that did not match the dynamic rule table&lt;br /&gt;
$cmd 332 deny tcp from any to any established in via $pif&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP's DHCP server as it's the only&lt;br /&gt;
# authorized source to send this packet type.&lt;br /&gt;
# Only necessary for cable or DSL configurations.&lt;br /&gt;
# This rule is not needed for 'user ppp' type connection to&lt;br /&gt;
# the public internet. This is the same IP address you captured&lt;br /&gt;
# and used in the outbound section.&lt;br /&gt;
$cmd 360 allow udp from x.x.x.x to any 68 in via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
$cmd 370 allow tcp from any to me 80 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
$cmd 380 allow tcp from any to me 22 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID &amp;amp; PW are passed over public&lt;br /&gt;
# internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
$cmd 390 allow tcp from any to me 23 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Reject &amp;amp; Log all unauthorized incoming connections from the public internet&lt;br /&gt;
$cmd 400 deny log all from any to any in via $pif&lt;br /&gt;
&lt;br /&gt;
# Reject &amp;amp; Log all unauthorized out going connections to the public internet&lt;br /&gt;
$cmd 450 deny log all from any to any out via $pif&lt;br /&gt;
&lt;br /&gt;
# This is skipto location for outbound stateful rules&lt;br /&gt;
$cmd 800 divert natd ip from any to any out via $pif&lt;br /&gt;
$cmd 801 allow ip from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Everything else is denied by default&lt;br /&gt;
# deny and log all packets that fell through to see what they are&lt;br /&gt;
$cmd 999 deny log all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
################ End of IPFW rules file ###############################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T22:31:43Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Example Inclusive Rule Set */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFW Rule Sets ==&lt;br /&gt;
A rule set is a group of ipfw rules coded to allow or deny packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc.) is predefined by its protocol and port number. This is the basic selection criteria used to create rules which will allow or deny services. &lt;br /&gt;
&lt;br /&gt;
When a packet enters the firewall it is compared against the first rule in the rule set and progresses one rule at a time, moving from top to bottom of the set in ascending rule number sequence order. When the packet matches a rule selection parameter, the rule's action field value is executed and the search of the rule set terminates for that packet. This is referred to as the 'first match wins' search method. If the packet does not match any of the rules, it gets caught by the mandatory ipfw default rule, number 65535 which denies all packets and discards them without any reply back to the originating destination. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section of the Installers Guide is based on using rules that contain the stateful ‘keep state’ and ‘limit’ options. This is the basic framework for coding an inclusive type firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destine for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is denied by default design. Inclusive firewalls are much more secure than exclusive firewall rule sets and are the only rule set type covered here in. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to what is necessary to create a standard inclusive type firewall rule set. For a complete rule syntax description see the online ‘man ipfw’ page at &lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/cgi/man.cgi?query=ipfw&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at &lt;br /&gt;
the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Syntax = CMD RULE# ACTION LOGGING SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
CMD Each rule has to be prefixed with the following to add the rule to the internal table,&lt;br /&gt;
&lt;br /&gt;
ipfw add&lt;br /&gt;
&lt;br /&gt;
RULE# Coding rule numbers is not a mandatory requirement. Rule numbers will automatically be assigned when the rules are loaded into the internal IPFW tables. Coding your own rule numbers means the numbers will not change during loading and gives you a fixed rule number which is listed in the log along with other information about the packet being logged. The rule number is how you relate the logged packet back to the rule that caused the packet to be logged. If a rule is entered without a number, ipfw will assign one. &lt;br /&gt;
&lt;br /&gt;
ACTIONS&lt;br /&gt;
&lt;br /&gt;
A rule can be associated with one of the following actions which will be &lt;br /&gt;
executed when the packet matches the selection criterion of the rule.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
allow | accept | pass | permit&lt;br /&gt;
&lt;br /&gt;
       These all mean the same thing which is to allow &lt;br /&gt;
       packets that match the rule to exit the firewall &lt;br /&gt;
       rule processing. The search terminates.&lt;br /&gt;
&lt;br /&gt;
check-state&lt;br /&gt;
&lt;br /&gt;
      Checks the packet against the dynamic rules table. &lt;br /&gt;
      If a match is found, execute the action associated with &lt;br /&gt;
      the rule which generated this dynamic rule, otherwise move &lt;br /&gt;
      to the next rule. The check-state rule does not have &lt;br /&gt;
      selection criteria. If no check-state rule is present in &lt;br /&gt;
      the rule set, the dynamic rules table is checked at the &lt;br /&gt;
      first keep-state or limit rule.&lt;br /&gt;
&lt;br /&gt;
deny | drop&lt;br /&gt;
&lt;br /&gt;
     Both words mean the same thing which is to discard packets &lt;br /&gt;
     that match this rule. The search terminates.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
LOGGING&lt;br /&gt;
&lt;br /&gt;
log or logamount number&lt;br /&gt;
&lt;br /&gt;
    When a packet matches a rule with the log keyword, a message will be&lt;br /&gt;
    logged to syslogd with a facility name of SECURITY. The logging &lt;br /&gt;
    only occurs if the number of packets logged so far for that&lt;br /&gt;
    particular rule does not exceed the logamount parameter. If no&lt;br /&gt;
    logamount is specified, the limit is taken from the sysctl variable&lt;br /&gt;
    net.inet.ip.fw.verbose_limit. In both cases, a value of zero removes&lt;br /&gt;
    the logging limit. Once the limit is reached, logging can be &lt;br /&gt;
    re-enabled by clearing the logging counter or the packet counter for&lt;br /&gt;
    that rule. See the ipfw reset log command.&lt;br /&gt;
    Note: logging is done after all other packet matching conditions &lt;br /&gt;
    have been successfully verified and before performing the final &lt;br /&gt;
    action accept, deny) on the packet. It’s up to you to decide which&lt;br /&gt;
    rules you want to enable logging on. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION&lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match the packet. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    udp | tcp | icmp  &lt;br /&gt;
        or any protocol names found in /etc/protocols are recognized &lt;br /&gt;
        and may be used. The value specified is the protocol to be&lt;br /&gt;
        matched against. This is a mandatory requirement. &lt;br /&gt;
&lt;br /&gt;
    from src to dst &lt;br /&gt;
        The from and to keywords are used to match against IP&lt;br /&gt;
        addresses. Rules must specify BOTH source and destination&lt;br /&gt;
        parameters. &lt;br /&gt;
        'any' is a special keyword that matches any IP address. &lt;br /&gt;
        'me' is a special keyword that matches any IP address&lt;br /&gt;
        configured on an interface in your FBSD system to represent the&lt;br /&gt;
        PC the firewall is running on. (IE: this box) &lt;br /&gt;
&lt;br /&gt;
        As in 'from me to any' or from 'any to me' or &lt;br /&gt;
        'from 0.0.0.0/0 to any' or from 'any to 0.0.0.0/0' or &lt;br /&gt;
        'from 0.0.0.0 to any' or 'from any to 0.0.0.0' or from &lt;br /&gt;
        'me to 0.0.0.0'  IP addresses are specified as a dotted IP&lt;br /&gt;
        address numeric form/mask-length or as single dotted IP address&lt;br /&gt;
        numeric form. &lt;br /&gt;
        This is a mandatory requirement. See this link for &lt;br /&gt;
        help on writing mask-lengths. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
    port number &lt;br /&gt;
        For protocols which support port numbers (such as TCP and UDP).&lt;br /&gt;
        It’s mandatory that you code the port number of the service &lt;br /&gt;
        you want to match on. Service names (from /etc/services) may be &lt;br /&gt;
        used instead of numeric port values. &lt;br /&gt;
&lt;br /&gt;
    in | out &lt;br /&gt;
        Matches incoming or outgoing packets, respectively. in and out&lt;br /&gt;
        are keywords and it’s mandatory that you code one or the other &lt;br /&gt;
        as part of your rule matching criterion.&lt;br /&gt;
&lt;br /&gt;
    via IFN &lt;br /&gt;
        Matches packets going through the interface specified by exact &lt;br /&gt;
        name. IFN = interface-name. The via keyword causes the interface&lt;br /&gt;
        to always be checked as part of the match process. &lt;br /&gt;
        via is mandatory.&lt;br /&gt;
&lt;br /&gt;
    setup &lt;br /&gt;
        This is a mandatory keyword that identifies the session start&lt;br /&gt;
        request for TCP packets.&lt;br /&gt;
&lt;br /&gt;
    keep-state &lt;br /&gt;
        This is a mandatory keyword. Upon a match, the firewall will&lt;br /&gt;
        create a dynamic rule whose default behavior is to match &lt;br /&gt;
        bidirectional traffic between source and destination IP/port&lt;br /&gt;
        using the same protocol. &lt;br /&gt;
&lt;br /&gt;
    limit {src-addr | src-port | dst-addr | dst-port}&lt;br /&gt;
        The firewall will only allow N connections with the same set of&lt;br /&gt;
        parameters as specified in the rule. One or more of source &lt;br /&gt;
        and destination addresses and ports can be specified. &lt;br /&gt;
        The ‘limit’ and 'keep-state’ cannot be used on same rule. &lt;br /&gt;
        Limit provides the same stateful function as ‘keep-state’ &lt;br /&gt;
        plus its own functions.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Option ==&lt;br /&gt;
&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors. This interrogation ability works for all the protocols. &lt;br /&gt;
&lt;br /&gt;
The 'check-state' &amp;lt;action&amp;gt; is used to identify where in the IPFW rules set the packet is to be tested against the dynamic rules facility. On a match the packet exits the firewall to continue on its way and a new rule is dynamic created for the next anticipated packet being exchanged during this bi-directional session conversation. On a no match the packet advances to the next rule in the rule set for testing.&lt;br /&gt;
&lt;br /&gt;
The dynamic rules facility is vulnerable to resource depletion from a SYN-flood attack which would open a huge number of dynamic rules. To counter this attack, FBSD version 4.5 added another new option named limit. This option is used to limit the number of simultaneous session conversations by interrogating the rule's source or destinations fields as directed by the limit option and using the packet's IP address found there. In a search of the open dynamic rules counting the number of times this rule and IP address combination occurred, if this count is greater that the value specified on the limit option, the packet is discarded.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Logging Firewall Messages ==&lt;br /&gt;
The benefits of logging are obvious, provides information like, what packets have been dropped, what addresses they came from, and where they were going. This gives you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPFW will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log verb to those rules. Normally only deny rules are logged, like the deny rule for incoming icmp pings. It's very customary to duplicate the ipfw default deny everything rule with the log verb included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
Logging is a two edged sword. If you're not careful, you can lose yourself in the over abundance of log data and fill all your free disk space with growing log files. DoS attacks that fill up disk drives is one of the oldest attacks around. These log messages are not only written to syslogd, but also are displayed on the root console screen and soon become very annoying.&lt;br /&gt;
&lt;br /&gt;
The IPFIREWALL_VERBOSE_LIMIT=5 kernel option limits the number of consecutive messages sent to the system logger syslogd concerning the packet matching of a given rule. When this option is enabled in the kernel, the number of consecutive messages concerning a particular rule is capped at the number specified. There is nothing to be gained from 200 log messages saying the same identical thing. For instance, 5 consecutive messages concerning a particular rule would be logged to syslogd, the remainder identical consecutive messages would be counted and posted to the syslogd with a phrase like this:&lt;br /&gt;
&lt;br /&gt;
last message repeated 45 times&lt;br /&gt;
&lt;br /&gt;
All logged packet messages are written by default to /var/log/security file, which is defined in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Most experienced IPFW users create a file containing the rules and code them in a manner compatible with running them as a script. The major benefit of doing this is the firewall rules can be refreshed in mass with out the need of rebooting the system to activate the new rules. This method is very convenient in testing new rules as the procedure can be executed as many times as needed. Being a script, you can use symbolic substitution to code frequent used values and substituting them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the 'sh', 'csh', 'tcsh' shells.&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
The value to populate the symbolic field must be enclosed in &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
############### start of example ipfw rules script ############# &lt;br /&gt;
# &lt;br /&gt;
ipfw –q -f flush     # Delete all rules&lt;br /&gt;
# Set defaults &lt;br /&gt;
oif=&amp;quot;tun0&amp;quot; # out interface &lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;      # ISP's dns server IP address&lt;br /&gt;
cmd=&amp;quot;ipfw -q add &amp;quot;     # build rule prefix&lt;br /&gt;
ks=&amp;quot;keep-state&amp;quot;        # just too lazy to key this each time&lt;br /&gt;
$cmd 00500 check-state&lt;br /&gt;
$cmd 00502 deny all from any to any frag&lt;br /&gt;
$cmd 00501 deny tcp from any to any established&lt;br /&gt;
$cmd 00600 allow tcp from any to any 80 out via $oif setup $ks&lt;br /&gt;
$cmd 00610 allow tcp from any to $odns 53 out via $oif setup $ks &lt;br /&gt;
$cmd 00611 allow udp from any to $odns 53 out via $oif $ks&lt;br /&gt;
################### End of example ipfw rules script ############&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's all there is to it. The rules are not important in this example; how the symbolic substitution field are populated and used are. &lt;br /&gt;
&lt;br /&gt;
If the above example was in /etc/ipfw.rules file, I could reload these rules by entering on the FBSD command &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sh /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
The /etc/ipfw.rules file could be located anywhere you want and the file could be named anything you wanted.&lt;br /&gt;
&lt;br /&gt;
The same thing could also to accomplished doing it this way as a text file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
ipfw -q add check-state&lt;br /&gt;
ipfw -q add deny all from any to any frag&lt;br /&gt;
ipfw -q add deny tcp from any to any established&lt;br /&gt;
ipfw -q add allow tcp from any to any 80 out via tun0 setup keep-state&lt;br /&gt;
ipfw -q add allow tcp from any to 192.0.2.11 53 out via tun0 setup keep-state &lt;br /&gt;
ipfw -q add 00611 allow udp from any to 192.0.2.11 53 out via tun0 keep-state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Set ==&lt;br /&gt;
 &lt;br /&gt;
The following non-NATed rule set is an example of how to code a very secure ‘inclusive’ type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication with in the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NICs are cabled to private LANs (local area networks) behind the firewall, those interfaces must have rules coded to allow free unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections: all the free unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘allow’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the proto, port, in/out, via and keep state options coded. The ‘proto tcp’ rules have the ‘setup’ option included to identify the start session request as the trigger packet to be posted to the keep state stateful table. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First is these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. The second reason is that by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; the packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php  and do a port number lookup to find what the purpose of that port number is. Check out this link for port numbers used for Trojans: http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Example Inclusive Rule Set ==&lt;br /&gt;
&lt;br /&gt;
The following non-NATed rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for you own. Just comment out any pass rules for services you don’t want. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing, just add a deny rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
You will see the pattern in the usage of these rules.&lt;br /&gt;
&lt;br /&gt;
All statements that are a request to start a session to the public Internet use keep-state.&lt;br /&gt;
&lt;br /&gt;
All the authorized services that originate from the public Internet have the limit option to stop flooding.&lt;br /&gt;
&lt;br /&gt;
All rules use in or out to clarify direction.&lt;br /&gt;
&lt;br /&gt;
All rules use via interface name to specify the interface the packet is traveling over.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################ Start of IPFW rules file ###############################&lt;br /&gt;
# Flush out the list before we begin.&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
&lt;br /&gt;
# Set rules command prefix&lt;br /&gt;
cmd=&amp;quot;ipfw -q add&amp;quot;&lt;br /&gt;
pif=&amp;quot;dc0&amp;quot;     # public interface name of Nic card &lt;br /&gt;
                        # facing the public Internet&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside Lan Interface for private network&lt;br /&gt;
# Not needed unless you have Lan. &lt;br /&gt;
# Change xl0 to your Lan Nic card interface name&lt;br /&gt;
################################################################# &lt;br /&gt;
#$cmd 00005 allow all from any to any via xl0 &lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
$cmd 00010 allow all from any to any via lo0 &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Allow the packet through if it has previous been added to the &lt;br /&gt;
# the &amp;quot;dynamic&amp;quot; rules table by an allow keep-state statement. &lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 00015 check-state&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network or from this gateway server&lt;br /&gt;
# destine for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# x.x.x.x must be the IP address of your ISP’s DNS&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
$cmd 00110 allow tcp from any to x.x.x.x 53 out via $pif setup keep-state &lt;br /&gt;
$cmd 00111 allow udp from any to x.x.x.x 53 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable/DSL configurations.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ connection to the public Internet.&lt;br /&gt;
# so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
$cmd 00120 allow log udp from any to any 67 out via $pif keep-state &lt;br /&gt;
#$cmd 00120 allow udp from any to x.x.x.x 67 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
$cmd 00200 allow tcp from any to any 80 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
$cmd 00220 allow tcp from any to any 443 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
$cmd 00230 allow tcp from any to any 25 out via $pif setup keep-state&lt;br /&gt;
$cmd 00231 allow tcp from any to any 110 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD (make install &amp;amp; CVSUP) functions&lt;br /&gt;
# Basically give user root &amp;quot;GOD&amp;quot; privileges.&lt;br /&gt;
$cmd 00240 allow tcp from me to any out via $pif setup keep-state uid root&lt;br /&gt;
&lt;br /&gt;
# Allow out ping &lt;br /&gt;
$cmd 00250 allow icmp from any to any out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
$cmd 00260 allow tcp from any to any 37 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news (IE: news groups)&lt;br /&gt;
$cmd 00270 allow tcp from any to any 119 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
$cmd 00280 allow tcp from any to any 22 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois &lt;br /&gt;
$cmd 00290 allow tcp from any to any 43 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# deny and log everything else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
$cmd 00299 deny log all from any to any out via $pif&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destine for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Deny all inbound traffic from non-routable reserved address spaces&lt;br /&gt;
$cmd 00300 deny all from 192.168.0.0/16 to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 00301 deny all from 172.16.0.0/12 to anyin via $pif    #RFC 1918 private IP&lt;br /&gt;
$cmd 00302 deny all from 10.0.0.0/8 to anyin via $pif       #RFC 1918 private IP&lt;br /&gt;
$cmd 00303 deny all from 127.0.0.0/8 to anyin via $pif      #loopback&lt;br /&gt;
$cmd 00304 deny all from 0.0.0.0/8 to anyin via $pif            #loopback&lt;br /&gt;
$cmd 00305 deny all from 169.254.0.0/16 to anyin via $pif   #DHCP auto-config&lt;br /&gt;
$cmd 00306 deny all from 192.0.2.0/24 to anyin via $pif     #reserved for doc's&lt;br /&gt;
$cmd 00307 deny all from 204.152.64.0/23 to anyin via $pif  #Sun cluster interconnect&lt;br /&gt;
$cmd 00308 deny all from 224.0.0.0/3 to anyin via $pif      #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
# Deny public pings&lt;br /&gt;
$cmd 00310 deny icmp from any to anyin via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny ident&lt;br /&gt;
$cmd 00315 deny tcp from any to any 113in via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
$cmd 00320 deny tcp from any to any 137in via $pif &lt;br /&gt;
$cmd 00321 deny tcp from any to any 138in via $pif &lt;br /&gt;
$cmd 00322 deny tcp from any to any 139in via $pif &lt;br /&gt;
$cmd 00323 deny tcp from any to any 81 in via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny any late arriving packets &lt;br /&gt;
$cmd 00330 deny all from any to any frag in via $pif&lt;br /&gt;
&lt;br /&gt;
# Deny ACK packets that did not match the dynamic rule table&lt;br /&gt;
$cmd 00332 deny tcp from any to any established in via $pif&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. &lt;br /&gt;
# Only necessary for cable or DSL configurations. &lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to &lt;br /&gt;
# the public Internet. This is the same IP address you captured &lt;br /&gt;
# and used in the outbound section.&lt;br /&gt;
#$cmd 00360 allow udp from any to x.x.x.x 67 in via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
$cmd 00400 allow tcp from any to me 80 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
$cmd 00410 allow tcp from any to me 22 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID &amp;amp; PW are passed over public&lt;br /&gt;
# Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
$cmd 00420 allow tcp from any to me 23 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Reject &amp;amp; Log all incoming connections from the outside&lt;br /&gt;
$cmd 00499 deny log all from any to any in via $pif&lt;br /&gt;
&lt;br /&gt;
# Everything else is denied by default &lt;br /&gt;
# deny and log all packets that fell through to see what they are&lt;br /&gt;
$cmd 00999 deny log all from any to any&lt;br /&gt;
################ End of IPFW rules file ###############################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T22:28:33Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFW Rule Sets ==&lt;br /&gt;
A rule set is a group of ipfw rules coded to allow or deny packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc.) is predefined by its protocol and port number. This is the basic selection criteria used to create rules which will allow or deny services. &lt;br /&gt;
&lt;br /&gt;
When a packet enters the firewall it is compared against the first rule in the rule set and progresses one rule at a time, moving from top to bottom of the set in ascending rule number sequence order. When the packet matches a rule selection parameter, the rule's action field value is executed and the search of the rule set terminates for that packet. This is referred to as the 'first match wins' search method. If the packet does not match any of the rules, it gets caught by the mandatory ipfw default rule, number 65535 which denies all packets and discards them without any reply back to the originating destination. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section of the Installers Guide is based on using rules that contain the stateful ‘keep state’ and ‘limit’ options. This is the basic framework for coding an inclusive type firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destine for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is denied by default design. Inclusive firewalls are much more secure than exclusive firewall rule sets and are the only rule set type covered here in. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to what is necessary to create a standard inclusive type firewall rule set. For a complete rule syntax description see the online ‘man ipfw’ page at &lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/cgi/man.cgi?query=ipfw&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at &lt;br /&gt;
the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Syntax = CMD RULE# ACTION LOGGING SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
CMD Each rule has to be prefixed with the following to add the rule to the internal table,&lt;br /&gt;
&lt;br /&gt;
ipfw add&lt;br /&gt;
&lt;br /&gt;
RULE# Coding rule numbers is not a mandatory requirement. Rule numbers will automatically be assigned when the rules are loaded into the internal IPFW tables. Coding your own rule numbers means the numbers will not change during loading and gives you a fixed rule number which is listed in the log along with other information about the packet being logged. The rule number is how you relate the logged packet back to the rule that caused the packet to be logged. If a rule is entered without a number, ipfw will assign one. &lt;br /&gt;
&lt;br /&gt;
ACTIONS&lt;br /&gt;
&lt;br /&gt;
A rule can be associated with one of the following actions which will be &lt;br /&gt;
executed when the packet matches the selection criterion of the rule.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
allow | accept | pass | permit&lt;br /&gt;
&lt;br /&gt;
       These all mean the same thing which is to allow &lt;br /&gt;
       packets that match the rule to exit the firewall &lt;br /&gt;
       rule processing. The search terminates.&lt;br /&gt;
&lt;br /&gt;
check-state&lt;br /&gt;
&lt;br /&gt;
      Checks the packet against the dynamic rules table. &lt;br /&gt;
      If a match is found, execute the action associated with &lt;br /&gt;
      the rule which generated this dynamic rule, otherwise move &lt;br /&gt;
      to the next rule. The check-state rule does not have &lt;br /&gt;
      selection criteria. If no check-state rule is present in &lt;br /&gt;
      the rule set, the dynamic rules table is checked at the &lt;br /&gt;
      first keep-state or limit rule.&lt;br /&gt;
&lt;br /&gt;
deny | drop&lt;br /&gt;
&lt;br /&gt;
     Both words mean the same thing which is to discard packets &lt;br /&gt;
     that match this rule. The search terminates.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
LOGGING&lt;br /&gt;
&lt;br /&gt;
log or logamount number&lt;br /&gt;
&lt;br /&gt;
    When a packet matches a rule with the log keyword, a message will be&lt;br /&gt;
    logged to syslogd with a facility name of SECURITY. The logging &lt;br /&gt;
    only occurs if the number of packets logged so far for that&lt;br /&gt;
    particular rule does not exceed the logamount parameter. If no&lt;br /&gt;
    logamount is specified, the limit is taken from the sysctl variable&lt;br /&gt;
    net.inet.ip.fw.verbose_limit. In both cases, a value of zero removes&lt;br /&gt;
    the logging limit. Once the limit is reached, logging can be &lt;br /&gt;
    re-enabled by clearing the logging counter or the packet counter for&lt;br /&gt;
    that rule. See the ipfw reset log command.&lt;br /&gt;
    Note: logging is done after all other packet matching conditions &lt;br /&gt;
    have been successfully verified and before performing the final &lt;br /&gt;
    action accept, deny) on the packet. It’s up to you to decide which&lt;br /&gt;
    rules you want to enable logging on. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION&lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match the packet. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    udp | tcp | icmp  &lt;br /&gt;
        or any protocol names found in /etc/protocols are recognized &lt;br /&gt;
        and may be used. The value specified is the protocol to be&lt;br /&gt;
        matched against. This is a mandatory requirement. &lt;br /&gt;
&lt;br /&gt;
    from src to dst &lt;br /&gt;
        The from and to keywords are used to match against IP&lt;br /&gt;
        addresses. Rules must specify BOTH source and destination&lt;br /&gt;
        parameters. &lt;br /&gt;
        'any' is a special keyword that matches any IP address. &lt;br /&gt;
        'me' is a special keyword that matches any IP address&lt;br /&gt;
        configured on an interface in your FBSD system to represent the&lt;br /&gt;
        PC the firewall is running on. (IE: this box) &lt;br /&gt;
&lt;br /&gt;
        As in 'from me to any' or from 'any to me' or &lt;br /&gt;
        'from 0.0.0.0/0 to any' or from 'any to 0.0.0.0/0' or &lt;br /&gt;
        'from 0.0.0.0 to any' or 'from any to 0.0.0.0' or from &lt;br /&gt;
        'me to 0.0.0.0'  IP addresses are specified as a dotted IP&lt;br /&gt;
        address numeric form/mask-length or as single dotted IP address&lt;br /&gt;
        numeric form. &lt;br /&gt;
        This is a mandatory requirement. See this link for &lt;br /&gt;
        help on writing mask-lengths. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
    port number &lt;br /&gt;
        For protocols which support port numbers (such as TCP and UDP).&lt;br /&gt;
        It’s mandatory that you code the port number of the service &lt;br /&gt;
        you want to match on. Service names (from /etc/services) may be &lt;br /&gt;
        used instead of numeric port values. &lt;br /&gt;
&lt;br /&gt;
    in | out &lt;br /&gt;
        Matches incoming or outgoing packets, respectively. in and out&lt;br /&gt;
        are keywords and it’s mandatory that you code one or the other &lt;br /&gt;
        as part of your rule matching criterion.&lt;br /&gt;
&lt;br /&gt;
    via IFN &lt;br /&gt;
        Matches packets going through the interface specified by exact &lt;br /&gt;
        name. IFN = interface-name. The via keyword causes the interface&lt;br /&gt;
        to always be checked as part of the match process. &lt;br /&gt;
        via is mandatory.&lt;br /&gt;
&lt;br /&gt;
    setup &lt;br /&gt;
        This is a mandatory keyword that identifies the session start&lt;br /&gt;
        request for TCP packets.&lt;br /&gt;
&lt;br /&gt;
    keep-state &lt;br /&gt;
        This is a mandatory keyword. Upon a match, the firewall will&lt;br /&gt;
        create a dynamic rule whose default behavior is to match &lt;br /&gt;
        bidirectional traffic between source and destination IP/port&lt;br /&gt;
        using the same protocol. &lt;br /&gt;
&lt;br /&gt;
    limit {src-addr | src-port | dst-addr | dst-port}&lt;br /&gt;
        The firewall will only allow N connections with the same set of&lt;br /&gt;
        parameters as specified in the rule. One or more of source &lt;br /&gt;
        and destination addresses and ports can be specified. &lt;br /&gt;
        The ‘limit’ and 'keep-state’ cannot be used on same rule. &lt;br /&gt;
        Limit provides the same stateful function as ‘keep-state’ &lt;br /&gt;
        plus its own functions.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Option ==&lt;br /&gt;
&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors. This interrogation ability works for all the protocols. &lt;br /&gt;
&lt;br /&gt;
The 'check-state' &amp;lt;action&amp;gt; is used to identify where in the IPFW rules set the packet is to be tested against the dynamic rules facility. On a match the packet exits the firewall to continue on its way and a new rule is dynamic created for the next anticipated packet being exchanged during this bi-directional session conversation. On a no match the packet advances to the next rule in the rule set for testing.&lt;br /&gt;
&lt;br /&gt;
The dynamic rules facility is vulnerable to resource depletion from a SYN-flood attack which would open a huge number of dynamic rules. To counter this attack, FBSD version 4.5 added another new option named limit. This option is used to limit the number of simultaneous session conversations by interrogating the rule's source or destinations fields as directed by the limit option and using the packet's IP address found there. In a search of the open dynamic rules counting the number of times this rule and IP address combination occurred, if this count is greater that the value specified on the limit option, the packet is discarded.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Logging Firewall Messages ==&lt;br /&gt;
The benefits of logging are obvious, provides information like, what packets have been dropped, what addresses they came from, and where they were going. This gives you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPFW will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log verb to those rules. Normally only deny rules are logged, like the deny rule for incoming icmp pings. It's very customary to duplicate the ipfw default deny everything rule with the log verb included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
Logging is a two edged sword. If you're not careful, you can lose yourself in the over abundance of log data and fill all your free disk space with growing log files. DoS attacks that fill up disk drives is one of the oldest attacks around. These log messages are not only written to syslogd, but also are displayed on the root console screen and soon become very annoying.&lt;br /&gt;
&lt;br /&gt;
The IPFIREWALL_VERBOSE_LIMIT=5 kernel option limits the number of consecutive messages sent to the system logger syslogd concerning the packet matching of a given rule. When this option is enabled in the kernel, the number of consecutive messages concerning a particular rule is capped at the number specified. There is nothing to be gained from 200 log messages saying the same identical thing. For instance, 5 consecutive messages concerning a particular rule would be logged to syslogd, the remainder identical consecutive messages would be counted and posted to the syslogd with a phrase like this:&lt;br /&gt;
&lt;br /&gt;
last message repeated 45 times&lt;br /&gt;
&lt;br /&gt;
All logged packet messages are written by default to /var/log/security file, which is defined in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Most experienced IPFW users create a file containing the rules and code them in a manner compatible with running them as a script. The major benefit of doing this is the firewall rules can be refreshed in mass with out the need of rebooting the system to activate the new rules. This method is very convenient in testing new rules as the procedure can be executed as many times as needed. Being a script, you can use symbolic substitution to code frequent used values and substituting them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the 'sh', 'csh', 'tcsh' shells.&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
The value to populate the symbolic field must be enclosed in &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
############### start of example ipfw rules script ############# &lt;br /&gt;
# &lt;br /&gt;
ipfw –q -f flush     # Delete all rules&lt;br /&gt;
# Set defaults &lt;br /&gt;
oif=&amp;quot;tun0&amp;quot; # out interface &lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;      # ISP's dns server IP address&lt;br /&gt;
cmd=&amp;quot;ipfw -q add &amp;quot;     # build rule prefix&lt;br /&gt;
ks=&amp;quot;keep-state&amp;quot;        # just too lazy to key this each time&lt;br /&gt;
$cmd 00500 check-state&lt;br /&gt;
$cmd 00502 deny all from any to any frag&lt;br /&gt;
$cmd 00501 deny tcp from any to any established&lt;br /&gt;
$cmd 00600 allow tcp from any to any 80 out via $oif setup $ks&lt;br /&gt;
$cmd 00610 allow tcp from any to $odns 53 out via $oif setup $ks &lt;br /&gt;
$cmd 00611 allow udp from any to $odns 53 out via $oif $ks&lt;br /&gt;
################### End of example ipfw rules script ############&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's all there is to it. The rules are not important in this example; how the symbolic substitution field are populated and used are. &lt;br /&gt;
&lt;br /&gt;
If the above example was in /etc/ipfw.rules file, I could reload these rules by entering on the FBSD command &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sh /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
The /etc/ipfw.rules file could be located anywhere you want and the file could be named anything you wanted.&lt;br /&gt;
&lt;br /&gt;
The same thing could also to accomplished doing it this way as a text file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
ipfw -q add check-state&lt;br /&gt;
ipfw -q add deny all from any to any frag&lt;br /&gt;
ipfw -q add deny tcp from any to any established&lt;br /&gt;
ipfw -q add allow tcp from any to any 80 out via tun0 setup keep-state&lt;br /&gt;
ipfw -q add allow tcp from any to 192.0.2.11 53 out via tun0 setup keep-state &lt;br /&gt;
ipfw -q add 00611 allow udp from any to 192.0.2.11 53 out via tun0 keep-state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Set ==&lt;br /&gt;
 &lt;br /&gt;
The following non-NATed rule set is an example of how to code a very secure ‘inclusive’ type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication with in the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NICs are cabled to private LANs (local area networks) behind the firewall, those interfaces must have rules coded to allow free unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections: all the free unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘allow’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the proto, port, in/out, via and keep state options coded. The ‘proto tcp’ rules have the ‘setup’ option included to identify the start session request as the trigger packet to be posted to the keep state stateful table. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First is these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. The second reason is that by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; the packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php  and do a port number lookup to find what the purpose of that port number is. Check out this link for port numbers used for Trojans: http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Example Inclusive Rule Set ==&lt;br /&gt;
&lt;br /&gt;
The following non-NATed rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for you own. Just comment out any pass rules for services you don’t want. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing, just add a deny rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
You will see the pattern in the usage of these rules.&lt;br /&gt;
&lt;br /&gt;
All statements that are a request to start a session to the public Internet use keep-state.&lt;br /&gt;
&lt;br /&gt;
All the authorized services that originate from the public Internet have the limit option to stop flooding.&lt;br /&gt;
&lt;br /&gt;
All rules use in or out to clarify direction.&lt;br /&gt;
&lt;br /&gt;
All rules use via interface name to specify the interface the packet is traveling over.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################ Start of IPFW rules file ###############################&lt;br /&gt;
# Flush out the list before we begin.&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
&lt;br /&gt;
# Set rules command prefix&lt;br /&gt;
cmd=&amp;quot;ipfw -q add&amp;quot;&lt;br /&gt;
pif=&amp;quot;dc0&amp;quot;     # public interface name of Nic card &lt;br /&gt;
                        # facing the public Internet&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside Lan Interface for private network&lt;br /&gt;
# Not needed unless you have Lan. &lt;br /&gt;
# Change xl0 to your Lan Nic card interface name&lt;br /&gt;
################################################################# &lt;br /&gt;
#$cmd 00005 allow all from any to any via xl0 &lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
$cmd 00010 allow all from any to any via lo0 &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Allow the packet through if it has previous been added to the &lt;br /&gt;
# the &amp;quot;dynamic&amp;quot; rules table by an allow keep-state statement. &lt;br /&gt;
#################################################################&lt;br /&gt;
$cmd 00015 check-state&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network or from this gateway server&lt;br /&gt;
# destine for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# x.x.x.x must be the IP address of your ISP’s DNS&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
$cmd 00110 allow tcp from any to x.x.x.x 53 out via $pif setup keep-state &lt;br /&gt;
$cmd 00111 allow udp from any to x.x.x.x 53 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable/DSL configurations.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ connection to the public Internet.&lt;br /&gt;
# so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
$cmd 00120 allow log udp from any to any 67 out via $pif keep-state &lt;br /&gt;
#$cmd 00120 allow udp from any to x.x.x.x 67 out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
$cmd 00200 allow tcp from any to any 80 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
$cmd 00220 allow tcp from any to any 443 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
$cmd 00230 allow tcp from any to any 25 out via $pif setup keep-state&lt;br /&gt;
$cmd 00231 allow tcp from any to any 110 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD (make install &amp;amp; CVSUP) functions&lt;br /&gt;
# Basically give user root &amp;quot;GOD&amp;quot; privileges.&lt;br /&gt;
$cmd 00240 allow tcp from me to any out via $pif setup keep-state uid root&lt;br /&gt;
&lt;br /&gt;
# Allow out ping &lt;br /&gt;
$cmd 00250 allow icmp from any to any out via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
$cmd 00260 allow tcp from any to any 37 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news (IE: news groups)&lt;br /&gt;
$cmd 00270 allow tcp from any to any 119 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
$cmd 00280 allow tcp from any to any 22 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois &lt;br /&gt;
$cmd 00290 allow tcp from any to any 43 out via $pif setup keep-state&lt;br /&gt;
&lt;br /&gt;
# deny and log everything else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
$cmd 00299 deny log all from any to any out via $pif&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destine for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Deny all inbound traffic from non-routable reserved address spaces&lt;br /&gt;
$cmd 00300 deny all from 192.168.0.0/16 to any in via $pif  #RFC 1918 private IP&lt;br /&gt;
$cmd 00301 deny all from 172.16.0.0/12 to anyin via $pif     #RFC 1918 private IP&lt;br /&gt;
$cmd 00302 deny all from 10.0.0.0/8 to anyin via $pif          #RFC 1918 private IP&lt;br /&gt;
$cmd 00303 deny all from 127.0.0.0/8 to anyin via $pif        #loopback&lt;br /&gt;
$cmd 00304 deny all from 0.0.0.0/8 to anyin via $pif            #loopback&lt;br /&gt;
$cmd 00305 deny all from 169.254.0.0/16 to anyin via $pif   #DHCP auto-config&lt;br /&gt;
$cmd 00306 deny all from 192.0.2.0/24 to anyin via $pif       #reserved for doc's&lt;br /&gt;
$cmd 00307 deny all from 204.152.64.0/23 to anyin via $pif  #Sun cluster interconnect&lt;br /&gt;
$cmd 00308 deny all from 224.0.0.0/3 to anyin via $pif         #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
# Deny public pings&lt;br /&gt;
$cmd 00310 deny icmp from any to anyin via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny ident&lt;br /&gt;
$cmd 00315 deny tcp from any to any 113in via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
$cmd 00320 deny tcp from any to any 137in via $pif &lt;br /&gt;
$cmd 00321 deny tcp from any to any 138in via $pif &lt;br /&gt;
$cmd 00322 deny tcp from any to any 139in via $pif &lt;br /&gt;
$cmd 00323 deny tcp from any to any 81 in via $pif &lt;br /&gt;
&lt;br /&gt;
# Deny any late arriving packets &lt;br /&gt;
$cmd 00330 deny all from any to any frag in via $pif&lt;br /&gt;
&lt;br /&gt;
# Deny ACK packets that did not match the dynamic rule table&lt;br /&gt;
$cmd 00332 deny tcp from any to any established in via $pif&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. &lt;br /&gt;
# Only necessary for cable or DSL configurations. &lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to &lt;br /&gt;
# the public Internet. This is the same IP address you captured &lt;br /&gt;
# and used in the outbound section.&lt;br /&gt;
#$cmd 00360 allow udp from any to x.x.x.x 67 in via $pif keep-state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
$cmd 00400 allow tcp from any to me 80 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
$cmd 00410 allow tcp from any to me 22 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID &amp;amp; PW are passed over public&lt;br /&gt;
# Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
$cmd 00420 allow tcp from any to me 23 in via $pif setup limit src-addr 2&lt;br /&gt;
&lt;br /&gt;
# Reject &amp;amp; Log all incoming connections from the outside&lt;br /&gt;
$cmd 00499 deny log all from any to any in via $pif&lt;br /&gt;
&lt;br /&gt;
# Everything else is denied by default &lt;br /&gt;
# deny and log all packets that fell through to see what they are&lt;br /&gt;
$cmd 00999 deny log all from any to any&lt;br /&gt;
################ End of IPFW rules file ###############################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T22:25:36Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Building Rule Script */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFW Rule Sets ==&lt;br /&gt;
A rule set is a group of ipfw rules coded to allow or deny packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc.) is predefined by its protocol and port number. This is the basic selection criteria used to create rules which will allow or deny services. &lt;br /&gt;
&lt;br /&gt;
When a packet enters the firewall it is compared against the first rule in the rule set and progresses one rule at a time, moving from top to bottom of the set in ascending rule number sequence order. When the packet matches a rule selection parameter, the rule's action field value is executed and the search of the rule set terminates for that packet. This is referred to as the 'first match wins' search method. If the packet does not match any of the rules, it gets caught by the mandatory ipfw default rule, number 65535 which denies all packets and discards them without any reply back to the originating destination. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section of the Installers Guide is based on using rules that contain the stateful ‘keep state’ and ‘limit’ options. This is the basic framework for coding an inclusive type firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destine for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is denied by default design. Inclusive firewalls are much more secure than exclusive firewall rule sets and are the only rule set type covered here in. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to what is necessary to create a standard inclusive type firewall rule set. For a complete rule syntax description see the online ‘man ipfw’ page at &lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/cgi/man.cgi?query=ipfw&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at &lt;br /&gt;
the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Syntax = CMD RULE# ACTION LOGGING SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
CMD Each rule has to be prefixed with the following to add the rule to the internal table,&lt;br /&gt;
&lt;br /&gt;
ipfw add&lt;br /&gt;
&lt;br /&gt;
RULE# Coding rule numbers is not a mandatory requirement. Rule numbers will automatically be assigned when the rules are loaded into the internal IPFW tables. Coding your own rule numbers means the numbers will not change during loading and gives you a fixed rule number which is listed in the log along with other information about the packet being logged. The rule number is how you relate the logged packet back to the rule that caused the packet to be logged. If a rule is entered without a number, ipfw will assign one. &lt;br /&gt;
&lt;br /&gt;
ACTIONS&lt;br /&gt;
&lt;br /&gt;
A rule can be associated with one of the following actions which will be &lt;br /&gt;
executed when the packet matches the selection criterion of the rule.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
allow | accept | pass | permit&lt;br /&gt;
&lt;br /&gt;
       These all mean the same thing which is to allow &lt;br /&gt;
       packets that match the rule to exit the firewall &lt;br /&gt;
       rule processing. The search terminates.&lt;br /&gt;
&lt;br /&gt;
check-state&lt;br /&gt;
&lt;br /&gt;
      Checks the packet against the dynamic rules table. &lt;br /&gt;
      If a match is found, execute the action associated with &lt;br /&gt;
      the rule which generated this dynamic rule, otherwise move &lt;br /&gt;
      to the next rule. The check-state rule does not have &lt;br /&gt;
      selection criteria. If no check-state rule is present in &lt;br /&gt;
      the rule set, the dynamic rules table is checked at the &lt;br /&gt;
      first keep-state or limit rule.&lt;br /&gt;
&lt;br /&gt;
deny | drop&lt;br /&gt;
&lt;br /&gt;
     Both words mean the same thing which is to discard packets &lt;br /&gt;
     that match this rule. The search terminates.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
LOGGING&lt;br /&gt;
&lt;br /&gt;
log or logamount number&lt;br /&gt;
&lt;br /&gt;
    When a packet matches a rule with the log keyword, a message will be&lt;br /&gt;
    logged to syslogd with a facility name of SECURITY. The logging &lt;br /&gt;
    only occurs if the number of packets logged so far for that&lt;br /&gt;
    particular rule does not exceed the logamount parameter. If no&lt;br /&gt;
    logamount is specified, the limit is taken from the sysctl variable&lt;br /&gt;
    net.inet.ip.fw.verbose_limit. In both cases, a value of zero removes&lt;br /&gt;
    the logging limit. Once the limit is reached, logging can be &lt;br /&gt;
    re-enabled by clearing the logging counter or the packet counter for&lt;br /&gt;
    that rule. See the ipfw reset log command.&lt;br /&gt;
    Note: logging is done after all other packet matching conditions &lt;br /&gt;
    have been successfully verified and before performing the final &lt;br /&gt;
    action accept, deny) on the packet. It’s up to you to decide which&lt;br /&gt;
    rules you want to enable logging on. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION&lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match the packet. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    udp | tcp | icmp  &lt;br /&gt;
        or any protocol names found in /etc/protocols are recognized &lt;br /&gt;
        and may be used. The value specified is the protocol to be&lt;br /&gt;
        matched against. This is a mandatory requirement. &lt;br /&gt;
&lt;br /&gt;
    from src to dst &lt;br /&gt;
        The from and to keywords are used to match against IP&lt;br /&gt;
        addresses. Rules must specify BOTH source and destination&lt;br /&gt;
        parameters. &lt;br /&gt;
        'any' is a special keyword that matches any IP address. &lt;br /&gt;
        'me' is a special keyword that matches any IP address&lt;br /&gt;
        configured on an interface in your FBSD system to represent the&lt;br /&gt;
        PC the firewall is running on. (IE: this box) &lt;br /&gt;
&lt;br /&gt;
        As in 'from me to any' or from 'any to me' or &lt;br /&gt;
        'from 0.0.0.0/0 to any' or from 'any to 0.0.0.0/0' or &lt;br /&gt;
        'from 0.0.0.0 to any' or 'from any to 0.0.0.0' or from &lt;br /&gt;
        'me to 0.0.0.0'  IP addresses are specified as a dotted IP&lt;br /&gt;
        address numeric form/mask-length or as single dotted IP address&lt;br /&gt;
        numeric form. &lt;br /&gt;
        This is a mandatory requirement. See this link for &lt;br /&gt;
        help on writing mask-lengths. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
    port number &lt;br /&gt;
        For protocols which support port numbers (such as TCP and UDP).&lt;br /&gt;
        It’s mandatory that you code the port number of the service &lt;br /&gt;
        you want to match on. Service names (from /etc/services) may be &lt;br /&gt;
        used instead of numeric port values. &lt;br /&gt;
&lt;br /&gt;
    in | out &lt;br /&gt;
        Matches incoming or outgoing packets, respectively. in and out&lt;br /&gt;
        are keywords and it’s mandatory that you code one or the other &lt;br /&gt;
        as part of your rule matching criterion.&lt;br /&gt;
&lt;br /&gt;
    via IFN &lt;br /&gt;
        Matches packets going through the interface specified by exact &lt;br /&gt;
        name. IFN = interface-name. The via keyword causes the interface&lt;br /&gt;
        to always be checked as part of the match process. &lt;br /&gt;
        via is mandatory.&lt;br /&gt;
&lt;br /&gt;
    setup &lt;br /&gt;
        This is a mandatory keyword that identifies the session start&lt;br /&gt;
        request for TCP packets.&lt;br /&gt;
&lt;br /&gt;
    keep-state &lt;br /&gt;
        This is a mandatory keyword. Upon a match, the firewall will&lt;br /&gt;
        create a dynamic rule whose default behavior is to match &lt;br /&gt;
        bidirectional traffic between source and destination IP/port&lt;br /&gt;
        using the same protocol. &lt;br /&gt;
&lt;br /&gt;
    limit {src-addr | src-port | dst-addr | dst-port}&lt;br /&gt;
        The firewall will only allow N connections with the same set of&lt;br /&gt;
        parameters as specified in the rule. One or more of source &lt;br /&gt;
        and destination addresses and ports can be specified. &lt;br /&gt;
        The ‘limit’ and 'keep-state’ cannot be used on same rule. &lt;br /&gt;
        Limit provides the same stateful function as ‘keep-state’ &lt;br /&gt;
        plus its own functions.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Option ==&lt;br /&gt;
&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors. This interrogation ability works for all the protocols. &lt;br /&gt;
&lt;br /&gt;
The 'check-state' &amp;lt;action&amp;gt; is used to identify where in the IPFW rules set the packet is to be tested against the dynamic rules facility. On a match the packet exits the firewall to continue on its way and a new rule is dynamic created for the next anticipated packet being exchanged during this bi-directional session conversation. On a no match the packet advances to the next rule in the rule set for testing.&lt;br /&gt;
&lt;br /&gt;
The dynamic rules facility is vulnerable to resource depletion from a SYN-flood attack which would open a huge number of dynamic rules. To counter this attack, FBSD version 4.5 added another new option named limit. This option is used to limit the number of simultaneous session conversations by interrogating the rule's source or destinations fields as directed by the limit option and using the packet's IP address found there. In a search of the open dynamic rules counting the number of times this rule and IP address combination occurred, if this count is greater that the value specified on the limit option, the packet is discarded.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Logging Firewall Messages ==&lt;br /&gt;
The benefits of logging are obvious, provides information like, what packets have been dropped, what addresses they came from, and where they were going. This gives you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPFW will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log verb to those rules. Normally only deny rules are logged, like the deny rule for incoming icmp pings. It's very customary to duplicate the ipfw default deny everything rule with the log verb included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
Logging is a two edged sword. If you're not careful, you can lose yourself in the over abundance of log data and fill all your free disk space with growing log files. DoS attacks that fill up disk drives is one of the oldest attacks around. These log messages are not only written to syslogd, but also are displayed on the root console screen and soon become very annoying.&lt;br /&gt;
&lt;br /&gt;
The IPFIREWALL_VERBOSE_LIMIT=5 kernel option limits the number of consecutive messages sent to the system logger syslogd concerning the packet matching of a given rule. When this option is enabled in the kernel, the number of consecutive messages concerning a particular rule is capped at the number specified. There is nothing to be gained from 200 log messages saying the same identical thing. For instance, 5 consecutive messages concerning a particular rule would be logged to syslogd, the remainder identical consecutive messages would be counted and posted to the syslogd with a phrase like this:&lt;br /&gt;
&lt;br /&gt;
last message repeated 45 times&lt;br /&gt;
&lt;br /&gt;
All logged packet messages are written by default to /var/log/security file, which is defined in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Most experienced IPFW users create a file containing the rules and code them in a manner compatible with running them as a script. The major benefit of doing this is the firewall rules can be refreshed in mass with out the need of rebooting the system to activate the new rules. This method is very convenient in testing new rules as the procedure can be executed as many times as needed. Being a script, you can use symbolic substitution to code frequent used values and substituting them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the 'sh', 'csh', 'tcsh' shells.&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
The value to populate the symbolic field must be enclosed in &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
############### start of example ipfw rules script ############# &lt;br /&gt;
# &lt;br /&gt;
ipfw –q -f flush     # Delete all rules&lt;br /&gt;
# Set defaults &lt;br /&gt;
oif=&amp;quot;tun0&amp;quot; # out interface &lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;      # ISP's dns server IP address&lt;br /&gt;
cmd=&amp;quot;ipfw -q add &amp;quot;     # build rule prefix&lt;br /&gt;
ks=&amp;quot;keep-state&amp;quot;        # just too lazy to key this each time&lt;br /&gt;
$cmd 00500 check-state&lt;br /&gt;
$cmd 00502 deny all from any to any frag&lt;br /&gt;
$cmd 00501 deny tcp from any to any established&lt;br /&gt;
$cmd 00600 allow tcp from any to any 80 out via $oif setup $ks&lt;br /&gt;
$cmd 00610 allow tcp from any to $odns 53 out via $oif setup $ks &lt;br /&gt;
$cmd 00611 allow udp from any to $odns 53 out via $oif $ks&lt;br /&gt;
################### End of example ipfw rules script ############&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's all there is to it. The rules are not important in this example; how the symbolic substitution field are populated and used are. &lt;br /&gt;
&lt;br /&gt;
If the above example was in /etc/ipfw.rules file, I could reload these rules by entering on the FBSD command &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sh /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
The /etc/ipfw.rules file could be located anywhere you want and the file could be named anything you wanted.&lt;br /&gt;
&lt;br /&gt;
The same thing could also to accomplished doing it this way as a text file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
ipfw -q add check-state&lt;br /&gt;
ipfw -q add deny all from any to any frag&lt;br /&gt;
ipfw -q add deny tcp from any to any established&lt;br /&gt;
ipfw -q add allow tcp from any to any 80 out via tun0 setup keep-state&lt;br /&gt;
ipfw -q add allow tcp from any to 192.0.2.11 53 out via tun0 setup keep-state &lt;br /&gt;
ipfw -q add 00611 allow udp from any to 192.0.2.11 53 out via tun0 keep-state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T22:23:18Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Rule Syntax */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFW Rule Sets ==&lt;br /&gt;
A rule set is a group of ipfw rules coded to allow or deny packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc.) is predefined by its protocol and port number. This is the basic selection criteria used to create rules which will allow or deny services. &lt;br /&gt;
&lt;br /&gt;
When a packet enters the firewall it is compared against the first rule in the rule set and progresses one rule at a time, moving from top to bottom of the set in ascending rule number sequence order. When the packet matches a rule selection parameter, the rule's action field value is executed and the search of the rule set terminates for that packet. This is referred to as the 'first match wins' search method. If the packet does not match any of the rules, it gets caught by the mandatory ipfw default rule, number 65535 which denies all packets and discards them without any reply back to the originating destination. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section of the Installers Guide is based on using rules that contain the stateful ‘keep state’ and ‘limit’ options. This is the basic framework for coding an inclusive type firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destine for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is denied by default design. Inclusive firewalls are much more secure than exclusive firewall rule sets and are the only rule set type covered here in. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to what is necessary to create a standard inclusive type firewall rule set. For a complete rule syntax description see the online ‘man ipfw’ page at &lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/cgi/man.cgi?query=ipfw&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at &lt;br /&gt;
the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Syntax = CMD RULE# ACTION LOGGING SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
CMD Each rule has to be prefixed with the following to add the rule to the internal table,&lt;br /&gt;
&lt;br /&gt;
ipfw add&lt;br /&gt;
&lt;br /&gt;
RULE# Coding rule numbers is not a mandatory requirement. Rule numbers will automatically be assigned when the rules are loaded into the internal IPFW tables. Coding your own rule numbers means the numbers will not change during loading and gives you a fixed rule number which is listed in the log along with other information about the packet being logged. The rule number is how you relate the logged packet back to the rule that caused the packet to be logged. If a rule is entered without a number, ipfw will assign one. &lt;br /&gt;
&lt;br /&gt;
ACTIONS&lt;br /&gt;
&lt;br /&gt;
A rule can be associated with one of the following actions which will be &lt;br /&gt;
executed when the packet matches the selection criterion of the rule.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
allow | accept | pass | permit&lt;br /&gt;
&lt;br /&gt;
       These all mean the same thing which is to allow &lt;br /&gt;
       packets that match the rule to exit the firewall &lt;br /&gt;
       rule processing. The search terminates.&lt;br /&gt;
&lt;br /&gt;
check-state&lt;br /&gt;
&lt;br /&gt;
      Checks the packet against the dynamic rules table. &lt;br /&gt;
      If a match is found, execute the action associated with &lt;br /&gt;
      the rule which generated this dynamic rule, otherwise move &lt;br /&gt;
      to the next rule. The check-state rule does not have &lt;br /&gt;
      selection criteria. If no check-state rule is present in &lt;br /&gt;
      the rule set, the dynamic rules table is checked at the &lt;br /&gt;
      first keep-state or limit rule.&lt;br /&gt;
&lt;br /&gt;
deny | drop&lt;br /&gt;
&lt;br /&gt;
     Both words mean the same thing which is to discard packets &lt;br /&gt;
     that match this rule. The search terminates.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
LOGGING&lt;br /&gt;
&lt;br /&gt;
log or logamount number&lt;br /&gt;
&lt;br /&gt;
    When a packet matches a rule with the log keyword, a message will be&lt;br /&gt;
    logged to syslogd with a facility name of SECURITY. The logging &lt;br /&gt;
    only occurs if the number of packets logged so far for that&lt;br /&gt;
    particular rule does not exceed the logamount parameter. If no&lt;br /&gt;
    logamount is specified, the limit is taken from the sysctl variable&lt;br /&gt;
    net.inet.ip.fw.verbose_limit. In both cases, a value of zero removes&lt;br /&gt;
    the logging limit. Once the limit is reached, logging can be &lt;br /&gt;
    re-enabled by clearing the logging counter or the packet counter for&lt;br /&gt;
    that rule. See the ipfw reset log command.&lt;br /&gt;
    Note: logging is done after all other packet matching conditions &lt;br /&gt;
    have been successfully verified and before performing the final &lt;br /&gt;
    action accept, deny) on the packet. It’s up to you to decide which&lt;br /&gt;
    rules you want to enable logging on. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION&lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match the packet. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    udp | tcp | icmp  &lt;br /&gt;
        or any protocol names found in /etc/protocols are recognized &lt;br /&gt;
        and may be used. The value specified is the protocol to be&lt;br /&gt;
        matched against. This is a mandatory requirement. &lt;br /&gt;
&lt;br /&gt;
    from src to dst &lt;br /&gt;
        The from and to keywords are used to match against IP&lt;br /&gt;
        addresses. Rules must specify BOTH source and destination&lt;br /&gt;
        parameters. &lt;br /&gt;
        'any' is a special keyword that matches any IP address. &lt;br /&gt;
        'me' is a special keyword that matches any IP address&lt;br /&gt;
        configured on an interface in your FBSD system to represent the&lt;br /&gt;
        PC the firewall is running on. (IE: this box) &lt;br /&gt;
&lt;br /&gt;
        As in 'from me to any' or from 'any to me' or &lt;br /&gt;
        'from 0.0.0.0/0 to any' or from 'any to 0.0.0.0/0' or &lt;br /&gt;
        'from 0.0.0.0 to any' or 'from any to 0.0.0.0' or from &lt;br /&gt;
        'me to 0.0.0.0'  IP addresses are specified as a dotted IP&lt;br /&gt;
        address numeric form/mask-length or as single dotted IP address&lt;br /&gt;
        numeric form. &lt;br /&gt;
        This is a mandatory requirement. See this link for &lt;br /&gt;
        help on writing mask-lengths. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
    port number &lt;br /&gt;
        For protocols which support port numbers (such as TCP and UDP).&lt;br /&gt;
        It’s mandatory that you code the port number of the service &lt;br /&gt;
        you want to match on. Service names (from /etc/services) may be &lt;br /&gt;
        used instead of numeric port values. &lt;br /&gt;
&lt;br /&gt;
    in | out &lt;br /&gt;
        Matches incoming or outgoing packets, respectively. in and out&lt;br /&gt;
        are keywords and it’s mandatory that you code one or the other &lt;br /&gt;
        as part of your rule matching criterion.&lt;br /&gt;
&lt;br /&gt;
    via IFN &lt;br /&gt;
        Matches packets going through the interface specified by exact &lt;br /&gt;
        name. IFN = interface-name. The via keyword causes the interface&lt;br /&gt;
        to always be checked as part of the match process. &lt;br /&gt;
        via is mandatory.&lt;br /&gt;
&lt;br /&gt;
    setup &lt;br /&gt;
        This is a mandatory keyword that identifies the session start&lt;br /&gt;
        request for TCP packets.&lt;br /&gt;
&lt;br /&gt;
    keep-state &lt;br /&gt;
        This is a mandatory keyword. Upon a match, the firewall will&lt;br /&gt;
        create a dynamic rule whose default behavior is to match &lt;br /&gt;
        bidirectional traffic between source and destination IP/port&lt;br /&gt;
        using the same protocol. &lt;br /&gt;
&lt;br /&gt;
    limit {src-addr | src-port | dst-addr | dst-port}&lt;br /&gt;
        The firewall will only allow N connections with the same set of&lt;br /&gt;
        parameters as specified in the rule. One or more of source &lt;br /&gt;
        and destination addresses and ports can be specified. &lt;br /&gt;
        The ‘limit’ and 'keep-state’ cannot be used on same rule. &lt;br /&gt;
        Limit provides the same stateful function as ‘keep-state’ &lt;br /&gt;
        plus its own functions.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Option ==&lt;br /&gt;
&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors. This interrogation ability works for all the protocols. &lt;br /&gt;
&lt;br /&gt;
The 'check-state' &amp;lt;action&amp;gt; is used to identify where in the IPFW rules set the packet is to be tested against the dynamic rules facility. On a match the packet exits the firewall to continue on its way and a new rule is dynamic created for the next anticipated packet being exchanged during this bi-directional session conversation. On a no match the packet advances to the next rule in the rule set for testing.&lt;br /&gt;
&lt;br /&gt;
The dynamic rules facility is vulnerable to resource depletion from a SYN-flood attack which would open a huge number of dynamic rules. To counter this attack, FBSD version 4.5 added another new option named limit. This option is used to limit the number of simultaneous session conversations by interrogating the rule's source or destinations fields as directed by the limit option and using the packet's IP address found there. In a search of the open dynamic rules counting the number of times this rule and IP address combination occurred, if this count is greater that the value specified on the limit option, the packet is discarded.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Logging Firewall Messages ==&lt;br /&gt;
The benefits of logging are obvious, provides information like, what packets have been dropped, what addresses they came from, and where they were going. This gives you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPFW will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log verb to those rules. Normally only deny rules are logged, like the deny rule for incoming icmp pings. It's very customary to duplicate the ipfw default deny everything rule with the log verb included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
Logging is a two edged sword. If you're not careful, you can lose yourself in the over abundance of log data and fill all your free disk space with growing log files. DoS attacks that fill up disk drives is one of the oldest attacks around. These log messages are not only written to syslogd, but also are displayed on the root console screen and soon become very annoying.&lt;br /&gt;
&lt;br /&gt;
The IPFIREWALL_VERBOSE_LIMIT=5 kernel option limits the number of consecutive messages sent to the system logger syslogd concerning the packet matching of a given rule. When this option is enabled in the kernel, the number of consecutive messages concerning a particular rule is capped at the number specified. There is nothing to be gained from 200 log messages saying the same identical thing. For instance, 5 consecutive messages concerning a particular rule would be logged to syslogd, the remainder identical consecutive messages would be counted and posted to the syslogd with a phrase like this:&lt;br /&gt;
&lt;br /&gt;
last message repeated 45 times&lt;br /&gt;
&lt;br /&gt;
All logged packet messages are written by default to /var/log/security file, which is defined in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Most experienced IPFW users create a file containing the rules and code them in a manner compatible with running them as a script. The major benefit of doing this is the firewall rules can be refreshed in mass with out the need of rebooting the system to activate the new rules. This method is very convenient in testing new rules as the procedure can be executed as many times as needed. Being a script, you can use symbolic substitution to code frequent used values and substituting them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the 'sh', 'csh', 'tcsh' shells.&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
The value to populate the symbolic field must be enclosed in &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
############### start of example ipfw rules script ############# &lt;br /&gt;
# &lt;br /&gt;
ipfw –q -f flush     # Delete all rules&lt;br /&gt;
# Set defaults &lt;br /&gt;
oif=&amp;quot;tun0&amp;quot; # out interface &lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;      # ISP's dns server IP address&lt;br /&gt;
cmd=&amp;quot;ipfw -q add &amp;quot;     # build rule prefix&lt;br /&gt;
ks=&amp;quot;keep-state&amp;quot;        # just too lazy to key this each time&lt;br /&gt;
$cmd 00500 check-state&lt;br /&gt;
$cmd 00502 deny all from any to any frag&lt;br /&gt;
$cmd 00501 deny tcp from any to any established&lt;br /&gt;
$cmd 00600 allow tcp from any to any 80 out via $oif setup $ks&lt;br /&gt;
$cmd 00610 allow tcp from any to $odns 53 out via $oif setup $ks &lt;br /&gt;
$cmd 00611 allow udp from any to $odns 53 out via $oif $ks&lt;br /&gt;
################### End of example ipfw rules script ############&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's all there is to it. The rules are not important in this example; how the symbolic substitution field are populated and used are. &lt;br /&gt;
&lt;br /&gt;
If the above example was in /etc/ipfw.rules file, I could reload these rules by entering on the FBSD command &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sh /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
The /etc/ipfw.rules file could be located anywhere you want and the file could be named anything you wanted.&lt;br /&gt;
&lt;br /&gt;
The same thing could also to accomplished doing it this way as a text file&lt;br /&gt;
&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
ipfw -q add check-state&lt;br /&gt;
ipfw -q add deny all from any to any frag&lt;br /&gt;
ipfw -q add deny tcp from any to any established&lt;br /&gt;
ipfw -q add allow tcp from any to any 80 out via tun0 setup keep-state&lt;br /&gt;
ipfw -q add allow tcp from any to 192.0.2.11 53 out via tun0 setup keep-state &lt;br /&gt;
ipfw -q add 00611 allow udp from any to 192.0.2.11 53 out via tun0 keep-state&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T22:00:39Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFW Rule Sets ==&lt;br /&gt;
A rule set is a group of ipfw rules coded to allow or deny packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc.) is predefined by its protocol and port number. This is the basic selection criteria used to create rules which will allow or deny services. &lt;br /&gt;
&lt;br /&gt;
When a packet enters the firewall it is compared against the first rule in the rule set and progresses one rule at a time, moving from top to bottom of the set in ascending rule number sequence order. When the packet matches a rule selection parameter, the rule's action field value is executed and the search of the rule set terminates for that packet. This is referred to as the 'first match wins' search method. If the packet does not match any of the rules, it gets caught by the mandatory ipfw default rule, number 65535 which denies all packets and discards them without any reply back to the originating destination. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section of the Installers Guide is based on using rules that contain the stateful ‘keep state’ and ‘limit’ options. This is the basic framework for coding an inclusive type firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destine for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is denied by default design. Inclusive firewalls are much more secure than exclusive firewall rule sets and are the only rule set type covered here in. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to what is necessary to create a standard inclusive type firewall rule set. For a complete rule syntax description see the online ‘man ipfw’ page at &lt;br /&gt;
&lt;br /&gt;
http://www.freebsd.org/cgi/man.cgi?query=ipfw&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Syntax = CMD RULE# ACTION LOGGING SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
CMD Each rule has to be prefixed with the following to add the rule to the internal table,&lt;br /&gt;
&lt;br /&gt;
ipfw add&lt;br /&gt;
&lt;br /&gt;
RULE# Coding rule numbers is not a mandatory requirement. Rule numbers will automatically be assigned when the rules are loaded into the internal IPFW tables. Coding your own rule numbers means the numbers will not change during loading and gives you a fixed rule number which is listed in the log along with other information about the packet being logged. The rule number is how you relate the logged packet back to the rule that caused the packet to be logged. If a rule is entered without a number, ipfw will assign one. &lt;br /&gt;
&lt;br /&gt;
ACTIONS&lt;br /&gt;
&lt;br /&gt;
A rule can be associated with one of the following actions which will be &lt;br /&gt;
executed when the packet matches the selection criterion of the rule.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
allow | accept | pass | permit&lt;br /&gt;
&lt;br /&gt;
       These all mean the same thing which is to allow &lt;br /&gt;
       packets that match the rule to exit the firewall &lt;br /&gt;
       rule processing. The search terminates.&lt;br /&gt;
&lt;br /&gt;
check-state&lt;br /&gt;
&lt;br /&gt;
      Checks the packet against the dynamic rules table. &lt;br /&gt;
      If a match is found, execute the action associated with &lt;br /&gt;
      the rule which generated this dynamic rule, otherwise move &lt;br /&gt;
      to the next rule. The check-state rule does not have &lt;br /&gt;
      selection criteria. If no check-state rule is present in &lt;br /&gt;
      the rule set, the dynamic rules table is checked at the &lt;br /&gt;
      first keep-state or limit rule.&lt;br /&gt;
&lt;br /&gt;
deny | drop&lt;br /&gt;
&lt;br /&gt;
     Both words mean the same thing which is to discard packets &lt;br /&gt;
     that match this rule. The search terminates.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
LOGGING&lt;br /&gt;
&lt;br /&gt;
log or logamount number&lt;br /&gt;
&lt;br /&gt;
    When a packet matches a rule with the log keyword, a message will be&lt;br /&gt;
    logged to syslogd with a facility name of SECURITY. The logging only occurs &lt;br /&gt;
    if the number of packets logged so far for that particular&lt;br /&gt;
    rule does not exceed the logamount parameter. If no logamount is&lt;br /&gt;
    specified, the limit is taken from the sysctl variable&lt;br /&gt;
    net.inet.ip.fw.verbose_limit. In both cases, a value of zero removes&lt;br /&gt;
    the logging limit. Once the limit is reached, logging can be &lt;br /&gt;
    re-enabled by clearing the logging counter or the packet counter for&lt;br /&gt;
    that rule. See the ipfw reset log command.&lt;br /&gt;
    Note: logging is done after all other packet matching conditions have&lt;br /&gt;
    been successfully verified and before performing the final action&lt;br /&gt;
    (accept, deny) on the packet. It’s up to you to decide which rules&lt;br /&gt;
    you want to enable logging on. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION&lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match the packet. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
    udp | tcp | icmp  &lt;br /&gt;
        or any protocol names found in /etc/protocols are recognized &lt;br /&gt;
        and may be used. The value specified is the protocol to be matched&lt;br /&gt;
        against. This is a mandatory requirement. &lt;br /&gt;
&lt;br /&gt;
    from src to dst &lt;br /&gt;
        The from and to keywords are used to match against IP addresses. &lt;br /&gt;
        Rules must specify BOTH source and destination parameters. &lt;br /&gt;
        any is a special keyword that matches any IP address. &lt;br /&gt;
        me is a special keyword that matches any IP address configured &lt;br /&gt;
        on an interface in your FBSD system to represent the PC &lt;br /&gt;
        the firewall is running on. (IE: this box) &lt;br /&gt;
&lt;br /&gt;
        As in from me to any or from any to me or from 0.0.0.0/0 to any &lt;br /&gt;
        or from any to 0.0.0.0/0 or from 0.0.0.0 to any or &lt;br /&gt;
        from any to 0.0.0.0 or from me to 0.0.0.0  IP addresses are &lt;br /&gt;
        specified as a dotted IP address numeric form/mask-length or &lt;br /&gt;
        as single dotted IP address numeric form. &lt;br /&gt;
        This is a mandatory requirement. See this link for &lt;br /&gt;
        help on writing mask-lengths. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
    port number &lt;br /&gt;
        For protocols which support port numbers (such as TCP and UDP).&lt;br /&gt;
        It’s mandatory that you code the port number of the service &lt;br /&gt;
        you want to match on. Service names (from /etc/services) may be &lt;br /&gt;
        used instead of numeric port values. &lt;br /&gt;
&lt;br /&gt;
    in | out &lt;br /&gt;
        Matches incoming or outgoing packets, respectively. in and out&lt;br /&gt;
        are keywords and it’s mandatory that you code one or the other &lt;br /&gt;
        as part of your rule matching criterion.&lt;br /&gt;
&lt;br /&gt;
    via IFN &lt;br /&gt;
        Matches packets going through the interface specified by exact &lt;br /&gt;
        name. IFN = interface-name. The via keyword causes the interface&lt;br /&gt;
        to always be checked as part of the match process. &lt;br /&gt;
        via is mandatory.&lt;br /&gt;
&lt;br /&gt;
    setup &lt;br /&gt;
        This is a mandatory keyword that identifies the session start&lt;br /&gt;
        request for TCP packets.&lt;br /&gt;
&lt;br /&gt;
    keep-state &lt;br /&gt;
        This is a mandatory keyword. Upon a match, the firewall will&lt;br /&gt;
        create a dynamic rule whose default behavior is to match &lt;br /&gt;
        bidirectional traffic between source and destination IP/port using&lt;br /&gt;
        the same protocol. &lt;br /&gt;
&lt;br /&gt;
    limit {src-addr | src-port | dst-addr | dst-port}&lt;br /&gt;
        The firewall will only allow N connections with the same set of&lt;br /&gt;
        parameters as specified in the rule. One or more of source &lt;br /&gt;
        and destination addresses and ports can be specified. &lt;br /&gt;
        The ‘limit’ and 'keep-state’ cannot be used on same rule. &lt;br /&gt;
        Limit provides the same stateful function as ‘keep-state’ &lt;br /&gt;
        plus its own functions. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stateful Rule Option ==&lt;br /&gt;
&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors. This interrogation ability works for all the protocols. &lt;br /&gt;
&lt;br /&gt;
The 'check-state' &amp;lt;action&amp;gt; is used to identify where in the IPFW rules set the packet is to be tested against the dynamic rules facility. On a match the packet exits the firewall to continue on its way and a new rule is dynamic created for the next anticipated packet being exchanged during this bi-directional session conversation. On a no match the packet advances to the next rule in the rule set for testing.&lt;br /&gt;
&lt;br /&gt;
The dynamic rules facility is vulnerable to resource depletion from a SYN-flood attack which would open a huge number of dynamic rules. To counter this attack, FBSD version 4.5 added another new option named limit. This option is used to limit the number of simultaneous session conversations by interrogating the rule's source or destinations fields as directed by the limit option and using the packet's IP address found there. In a search of the open dynamic rules counting the number of times this rule and IP address combination occurred, if this count is greater that the value specified on the limit option, the packet is discarded.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Logging Firewall Messages ==&lt;br /&gt;
The benefits of logging are obvious, provides information like, what packets have been dropped, what addresses they came from, and where they were going. This gives you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPFW will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log verb to those rules. Normally only deny rules are logged, like the deny rule for incoming icmp pings. It's very customary to duplicate the ipfw default deny everything rule with the log verb included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
Logging is a two edged sword. If you're not careful, you can lose yourself in the over abundance of log data and fill all your free disk space with growing log files. DoS attacks that fill up disk drives is one of the oldest attacks around. These log messages are not only written to syslogd, but also are displayed on the root console screen and soon become very annoying.&lt;br /&gt;
&lt;br /&gt;
The IPFIREWALL_VERBOSE_LIMIT=5 kernel option limits the number of consecutive messages sent to the system logger syslogd concerning the packet matching of a given rule. When this option is enabled in the kernel, the number of consecutive messages concerning a particular rule is capped at the number specified. There is nothing to be gained from 200 log messages saying the same identical thing. For instance, 5 consecutive messages concerning a particular rule would be logged to syslogd, the remainder identical consecutive messages would be counted and posted to the syslogd with a phrase like this:&lt;br /&gt;
&lt;br /&gt;
last message repeated 45 times&lt;br /&gt;
&lt;br /&gt;
All logged packet messages are written by default to /var/log/security file, which is defined in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Most experienced IPFW users create a file containing the rules and code them in a manner compatible with running them as a script. The major benefit of doing this is the firewall rules can be refreshed in mass with out the need of rebooting the system to activate the new rules. This method is very convenient in testing new rules as the procedure can be executed as many times as needed. Being a script, you can use symbolic substitution to code frequent used values and substituting them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the 'sh', 'csh', 'tcsh' shells.&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
The value to populate the symbolic field must be enclosed in &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
############### start of example ipfw rules script ############# &lt;br /&gt;
# &lt;br /&gt;
ipfw –q -f flush     # Delete all rules&lt;br /&gt;
# Set defaults &lt;br /&gt;
oif=&amp;quot;tun0&amp;quot; # out interface &lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;      # ISP's dns server IP address&lt;br /&gt;
cmd=&amp;quot;ipfw -q add &amp;quot;     # build rule prefix&lt;br /&gt;
ks=&amp;quot;keep-state&amp;quot;        # just too lazy to key this each time&lt;br /&gt;
$cmd 00500 check-state&lt;br /&gt;
$cmd 00502 deny all from any to any frag&lt;br /&gt;
$cmd 00501 deny tcp from any to any established&lt;br /&gt;
$cmd 00600 allow tcp from any to any 80 out via $oif setup $ks&lt;br /&gt;
$cmd 00610 allow tcp from any to $odns 53 out via $oif setup $ks &lt;br /&gt;
$cmd 00611 allow udp from any to $odns 53 out via $oif $ks&lt;br /&gt;
################### End of example ipfw rules script ############&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's all there is to it. The rules are not important in this example; how the symbolic substitution field are populated and used are. &lt;br /&gt;
&lt;br /&gt;
If the above example was in /etc/ipfw.rules file, I could reload these rules by entering on the FBSD command &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sh /etc/ipfw.rules&lt;br /&gt;
&lt;br /&gt;
The /etc/ipfw.rules file could be located anywhere you want and the file could be named anything you wanted.&lt;br /&gt;
&lt;br /&gt;
The same thing could also to accomplished doing it this way as a text file&lt;br /&gt;
&lt;br /&gt;
ipfw -q -f flush&lt;br /&gt;
ipfw -q add check-state&lt;br /&gt;
ipfw -q add deny all from any to any frag&lt;br /&gt;
ipfw -q add deny tcp from any to any established&lt;br /&gt;
ipfw -q add allow tcp from any to any 80 out via tun0 setup keep-state&lt;br /&gt;
ipfw -q add allow tcp from any to 192.0.2.11 53 out via tun0 setup keep-state &lt;br /&gt;
ipfw -q add 00611 allow udp from any to 192.0.2.11 53 out via tun0 keep-state&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T21:54:18Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* IPFW Command */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T21:52:22Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Kernel options */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more   &lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T21:50:51Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Enabling IPFW */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more   &lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall</id>
		<title>IPFIREWALL (IPFW) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFIREWALL_(IPFW)_Firewall"/>
				<updated>2012-08-13T21:49:23Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: IPFIREWALL (IPFW) Firewall&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFIREWALL (IPFW) Firewall ==&lt;br /&gt;
 &lt;br /&gt;
The IPFIREWALL (IPFW) is a FBSD sponsored firewall software application authored and maintained by FBSD volunteer staff members. It uses the legacy stateless rules and a legacy rule coding technique to achieve what is referred to as simple stateful logic. &lt;br /&gt;
&lt;br /&gt;
The IPFW stateless rule syntax is empowered with technically sophisticated selection capabilities which far surpasses the knowledge level of the customary firewall installer. IPFW is targeted at the professional user or the advanced technical computer hobbyist who has advanced packet selection requirements. A high degree of detailed knowledge into how different protocols use and create their unique packet header information is necessary before the power of the IPFW rules can be unleashed. Providing that level of explanation is out of the scope of this section of the handbook. &lt;br /&gt;
&lt;br /&gt;
IPFW is composed of seven components; the kernel firewall filter rule processor and its integrated packet accounting facility (the primary component), the logging facility, the ‘divert’ rule which triggers the NAT facility, and the advanced special purpose facilities (the dummynet traffic shaper facilities the ‘fwd rule’ forward facility, the bridge facility, and the ipstealth facility). &lt;br /&gt;
&lt;br /&gt;
See the FBSD man pages, 'man ipfw' or 'man ipfirewall' or 'man dummynet' for details.&lt;br /&gt;
&lt;br /&gt;
From this point on I will use IPFW to mean IPFIREWALL. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPFW ==&lt;br /&gt;
IPFW is included in the basic FBSD install as a separate run time loadable module. IPFW will dynamically load its kernel loadable module when the rc.conf statement firewall_enable=&amp;quot;YES&amp;quot; is used. You do not need to compile IPFW into the FBSD kernel. &lt;br /&gt;
&lt;br /&gt;
Using the IPFW run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
After rebooting your system with firewall_enable=&amp;quot;YES&amp;quot; in rc.conf the following white highlighted message is displayed on the screen as part of the boot process.&lt;br /&gt;
&lt;br /&gt;
IP packet filtering initialized, divert disabled, rule-based forwarding &lt;br /&gt;
enabled, default to deny, logging disabled &lt;br /&gt;
&lt;br /&gt;
You can disregard this message as it’s outdated and no longer is the true status of the IPFW loadable module. The loadable module really does have logging ability.&lt;br /&gt;
&lt;br /&gt;
To set the verbose limit, there is a knob you can set in sysctl.conf by adding this statement to the file:&lt;br /&gt;
&lt;br /&gt;
    ee /etc/sysctl.conf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    net.inet.ip.fw.verbose_limit=5&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPFW by compiling the following options into the FBSD kernel. It’s only presented here as a background information option. Compiling IPFW into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPFW options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFIREWALL&lt;br /&gt;
options IPFIREWALL_VERBOSE &lt;br /&gt;
options IPFIREWALL_VERBOSE_LIMIT=5&lt;br /&gt;
option  IPDIVERT               &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL This tells the compile to include IPFW as part of the kernel. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE enables the option to have IPFW log traffic by printing packet activity to syslogd for every rule that has the &amp;quot;log&amp;quot; keyword. &lt;br /&gt;
&lt;br /&gt;
IPFIREWALL_VERBOSE_LIMIT=5 specifies the default number of packets from a particular rule is to be logged. Without this option each repeated occurrences of the same packet will be logged and eventually consume all the free disk space, resulting in services being denied due to lack of resources. The 5 is the number of consecutive times to log evidence of this unique occurrence.&lt;br /&gt;
&lt;br /&gt;
IPDIVERT adds the userland natd function which is utilized by the divert natd IPFW rule statement.&lt;br /&gt;
&lt;br /&gt;
A complete list of the IPFW options statements are in /usr/src/sys/i386/conf/LINT &lt;br /&gt;
&lt;br /&gt;
Installer note: After compiling IPFW into your kernel you lose the ability to access all private LAN and public Internet networks, until you enable IPFW in rc.conf and reboot. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You have to tell FBSD to active it at boot time. You do that by adding the following statements to /etc/rc.conf: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
firewall_enable=&amp;quot;YES&amp;quot;               # Start IPFW daemon&lt;br /&gt;
firewall_script=&amp;quot;/etc/ipfw.rules&amp;quot;   # Use my custom rules.&lt;br /&gt;
filewall_logging=&amp;quot;YES&amp;quot;              # Enable packet logging&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For a completely open firewall, you have to create the /etc/ipfw.rules file with the following rules &lt;br /&gt;
&lt;br /&gt;
ipfw –f flush &lt;br /&gt;
ipfw add allow all from any to any&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPFW Command ==&lt;br /&gt;
The ipfw command is the normal vehicle for making manual single rule additions or deletions to the firewall active internal rules while it's running. The problem with using this method is once your system is shutdown or halted, all the rules you added or changed or deleted are lost. Writing all your rules in a file and using that file to load the rules at boot time or to replace in mass the currently running firewall rules with changes you made to the files content is the recommended method used here. &lt;br /&gt;
&lt;br /&gt;
The ipfw command is still a very useful for displaying the running firewall rules to the console screen. The IPFW accounting facility dynamically creates a counter for each rule that counts each packet that matches the rule. During the process of testing a rule, listing the rule with its counter is the only way of determining if the rule is functioning. &lt;br /&gt;
&lt;br /&gt;
You would enter on the FBSD command line one of the following forms of the list command.&lt;br /&gt;
&lt;br /&gt;
ipfw list          List all rules in rule number sequence.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -t list       List rules in rule number sequence with timestamp &lt;br /&gt;
                   of last time that rule was matched.&lt;br /&gt;
                        &lt;br /&gt;
ipfw -a list       List the accounting information, packet count for &lt;br /&gt;
                   matched rules along with the rules themselves. &lt;br /&gt;
                   The first column is the rule number, followed &lt;br /&gt;
                   by the number of outgoing matched packets, &lt;br /&gt;
                   followed by the number of incoming matched packets, &lt;br /&gt;
                   and finally followed by the rule itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ipfw -d list       List dynamic rules in addition to static ones.&lt;br /&gt;
&lt;br /&gt;
ipfw -d -e list    Also show expired dynamic rules.&lt;br /&gt;
&lt;br /&gt;
ipfw zero          Clear all the accounting counters.&lt;br /&gt;
&lt;br /&gt;
ipfw zero number   Clear accounting counter just for this rule number.&lt;br /&gt;
&lt;br /&gt;
ipfw show | more   &lt;br /&gt;
&lt;br /&gt;
If you have a big rule set with dynamic rules it will scroll off the screen. Suffix the command with ‘ | more’ which will only display the first screen full, and then you have to use the arrow keys or enter key to scroll down through the info.&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:38:50Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* RC.CONF Options */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above.&lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:38:04Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Kernel options */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above.&lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:37:25Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Enabling IPF */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above.&lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:36:48Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* IPMON Logging */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above.&lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:36:08Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* IPMON Command */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:35:31Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* IPFILTER (IPF) Firewall */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:35:00Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* IPFSTAT Command */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:34:18Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* IPF COMMAND */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:33:38Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* RC.CONF Options */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:32:57Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Kernel options */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:29:19Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* FTP Special NAT Handling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
To build a custom kernel see the Kernel Customizing section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed.&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:26:59Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
To build a custom kernel see the Kernel Customizing section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Explanation ==&lt;br /&gt;
NAT stands for Network Address Translation. To those familiar with Linux, this concept is called IP Masquerading; NAT and IP Masquerading are the same thing. One of the many things the IPF NAT function enables is the ability to have a private local area network (LAN) behind the firewall sharing a single ISP assigned IP address to the public Internet. &lt;br /&gt;
&lt;br /&gt;
You ask why would someone want to do this. ISP’s normally assign a dynamic IP address to their non-commercial users. Dynamic means the IP address can be different each time you dial in and logon to your ISP, or for cable and DSL modem users when you power off and then power on your modems you can get assigned a different IP address. This IP address is how you are known to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Now lets say you have 5 PC’s at home and each one needs Internet access. You would have to pay your ISP for an individual Internet account for each PC and have 5 phone lines. &lt;br /&gt;
&lt;br /&gt;
With NAT you only need a single account with your ISP, then cable your other 4 PC’s to a switch and the switch to the NIC in your FBSD system which is going to service your LAN as a gateway. NAT will automatically translate the private LAN IP address for each separate PC on the LAN to the single public IP address as it exits the firewall bound for the public Internet. It also does the reverse translation for returning packets. &lt;br /&gt;
&lt;br /&gt;
NAT is most often accomplished without the approval, or knowledge, of your ISP, and in most cases is grounds for your ISP terminating your account if found out. Commercial users pay a lot more for their Internet connection and usually get assigned a block of static IP addresses which never change. The ISP also expects and consents to their commercial customers using NAT for their internal private LANs. &lt;br /&gt;
&lt;br /&gt;
There is a special range of IP addresses reserved for NATed private LAN IP addresses.&lt;br /&gt;
&lt;br /&gt;
According to RFC 1918, you can use the following IP ranges for private nets which will never be routed directly to the public Internet.&lt;br /&gt;
&lt;br /&gt;
Start IP 10.0.0.0    - Ending IP 10.255.255.255&lt;br /&gt;
Start IP 172.16.0.0  - Ending IP 172.31.255.255&lt;br /&gt;
Start IP 192.168.0.0 - Ending IP 192.168.255.255&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPNAT Command ==&lt;br /&gt;
NAT rules are loaded by using the ipnat command. Typically the NAT rules are stored in /etc/ipnat.rules. See man ipnat(1) for details.&lt;br /&gt;
&lt;br /&gt;
When changing the NAT rules after NAT has been started, make your changes to the file containing the NAT rules, then run the ipnat command with the –CF flags to delete the internal in use NAT rules and flush the contents of the translation table of all active entries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipnat –CF –f /etc/ipnat.rules   # reload the NAT rules&lt;br /&gt;
&lt;br /&gt;
ipnat -s       # Retrieve and display NAT statistics&lt;br /&gt;
&lt;br /&gt;
ipnat -l       # List the internal NAT table entry mappings.&lt;br /&gt;
&lt;br /&gt;
ipnat -v       # Turn verbose mode on to display information                  relating to rule processing and active rules/table entries. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT Rules ==&lt;br /&gt;
NAT rules are very flexible and can accomplish many different things to fit the needs of non-commercial users with a single dynamic IP address or commercial users who have blocks of static IP address ranges assigned to them. &lt;br /&gt;
&lt;br /&gt;
The rule syntax presented here has been simplified to what is most commonly used in a non-commercial environment. For a complete rule syntax description see the man ipnat page at ipnat(5) or ipnat(8). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. &lt;br /&gt;
&lt;br /&gt;
These special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
For standard NAT functionality, you only need a single NAT rule. &lt;br /&gt;
&lt;br /&gt;
Create a file called /etc/ipnat.rules with the following line:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map = The keyword map starts the rule.&lt;br /&gt;
&lt;br /&gt;
dc0 = The interface name of the interface facing the public Internet.&lt;br /&gt;
&lt;br /&gt;
10.0.10.1/29 = The IP address range of the private LAN. &lt;br /&gt;
&lt;br /&gt;
-&amp;gt; = Mandatory arrow symbol.&lt;br /&gt;
&lt;br /&gt;
0.32 = The IP address/netmask assigned by your ISP.&lt;br /&gt;
       The special alias keyword 0.32 tells ipnat to get the current public &lt;br /&gt;
       IP address of the interface specified on this statement and &lt;br /&gt;
       substitute it for the 0.32 keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== How NAT works ==&lt;br /&gt;
A packet leaves a LAN PC for the public internet carring it's private LAN source IP address and public destination IP address. It passes through the outbound firewall rules and NAT gets it's turn at the packet and applies its rules top down; the first matching rule wins. NAT tests each of its rules against the packet's interface name and source IP address. When a packet's interface name matches a NAT rule then the source IP address (IE: private LAN IP address) of the packet is checked to see if it falls within the IP address range specified to the left of the arrow symbol on the NAT rule. On a match the packet has its source IP address rewritten with the public IP address obtained by the 0.32 keyword. NAT posts an entry in its internal NAT table so when the packet returns from the public Internet it can be mapped back to its original private IP address and then passed to the filter rules for processing.&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling NAT ==&lt;br /&gt;
To enable the IPNAT function add these statements to /etc/rc.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # ipnat rules definition file&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NAT for very large LAN ==&lt;br /&gt;
For networks that have large numbers of PC's on the LAN or networks with more that a single LAN, the process of funneling all those private IP addresses into a single public IP address becomes a resource problem that may cause problems with same port numbers being used many times across many NATd LAN PC's causing collisions. There are 2 ways to relieve this resource problem. &lt;br /&gt;
&lt;br /&gt;
1. Mapping many LAN addresses into a single public address.&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32&lt;br /&gt;
&lt;br /&gt;
In the above rule the packet's source port is unchanged from the original source port. IPNAT has the special keyword &amp;quot;portmap&amp;quot; that changes the above rule into&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp 20000:6000&lt;br /&gt;
&lt;br /&gt;
This rule now shoehorns all the translated connections (which can be tcp, udp, or tcp/udp) into the port range of 20000 to 60000.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Additionally, we can make things even easier by using the &amp;quot;auto&amp;quot; keyword to tell ipnat to determine for itself which ports are available for use and allocate a proportional amount of them per address in your pool versus addresses being NATed:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 0.32 portmap tcp/udp auto&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
2. Mapping many LAN addresses into a pool of static public addresses.&lt;br /&gt;
&lt;br /&gt;
In large LANs there comes a point where there are just too many LAN addresses to fit into a single public IP address. Change the map rule to specify a range of public IP addresses as follows:&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp 20000:60000    &lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.1/29 -&amp;gt; 20.20.20.0/24 portmap tcp/udp auto           &lt;br /&gt;
&lt;br /&gt;
Here 20.20.20.0/24 is the pool of static public IP addresses assigned to you by your ISP. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Directing traffic to LAN servers ==&lt;br /&gt;
 &lt;br /&gt;
An very common practice is to have a web server, email server, database server or domain name server each segregated to a different PC on the LAN. Any traffic originating from those servers destine for the public internet would pass out the firewall on keep state rules automatically controlling the bidirectional exchange of packets for the duration of the session conversation. Due to the nature of these servers they also need to receive unsolicited inbound traffic from the public internet. The problem is how to direct this unsolicited inbound traffic to the correct target PC on the LAN?&lt;br /&gt;
&lt;br /&gt;
For this purpose we use the NAT 'rdr' directive in the /etc/ipnat.rules file to instruct where to redirect (or route) a particular packet to on the NAT'ed LAN.&lt;br /&gt;
&lt;br /&gt;
For example, lets say your web server resides on the LAN and you want it to be accessible from the public internet. The NAT rules file would need a additional rule added after the MAP rules to handle this. You would code the rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        rdr xl0 0.0.0.0/0 port 80 -&amp;gt; 10.0.10.5 port 80&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;quot;rdr&amp;quot; = the command which provides the selection information and target redirect information. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;xl0&amp;quot; = is the network interface that is connected to the public internet.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;0.0.0.0/0&amp;quot; = is a special aliases which means any source IP address contained in the inbound packet traveling the interface is to be selected for testing. The other special aliases |0.32|, |0/0| and |0/32|, only work in IPNAT's map and bimap rules. They do NOT work in IPF rules, or in IPNAT rdr rules and 0.0.0.0/0 only works in rdr rules. Beware how and where you use these special aliases as incorrect usage generates no errors. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; = is the destination port number that has to be matched in the inbound packet to select the candidate packet to be redirected. The number &amp;quot;80&amp;quot; don't have to be used. You can use &amp;quot;port www&amp;quot; to specify a redirection of port 80. If you would like to use a name instead of a number, the service name and corresponding port, must exist in the file /etc/services.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;-&amp;gt;&amp;quot; = Mandatory arrow symbol used to distinguish between the rule selection information side and the redirect information side of the rule.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;quot;10.0.10.5&amp;quot; = the IP address of the LAN PC which the matched packets are to be targeted to. The netmask defaults to &amp;quot;/32&amp;quot; and therefore should not be coded.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;port 80&amp;quot; - this is the port number value to substitute in the destination port number of the redirected packet. You could make it 8088 and tell the web server on 10.0.10.5 to listen on that port number. If omitted the destination port number goes unchanged.&lt;br /&gt;
&lt;br /&gt;
The above IPNAT redirect rule says that any inbound packet that has not already had it's destination IP address translated by earlier MAP rules will then have it's destination port tested for match to the port number on the left of the mandatory arrow symbol, if  matched, the packet's destination IP address and port number are over written with the values on the right of the mandatory arrow symbol and then released for processing against the firewall rules. NAT then posts an entry in its internal NAT table so when the packet returns after first being processed by the firewall rules on it's outbound journey it can have it's private LAN IP address mapped back to your gateway's public routable IP address. After reloading the NAT rules, the redirection will start immediately.&lt;br /&gt;
&lt;br /&gt;
Depending on how tightly or loosely you control the services by firewall rules the redirected packet may require a firewall rule to let it pass. &lt;br /&gt;
Such as this for the above example:&lt;br /&gt;
&lt;br /&gt;
pass in quick on xl0 proto tcp from any to 10.0.10.5 port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
The main thing to remember is the firewall rules only sees the non-public routable private LAN IP address. The NAT function happens after the outbound packet has been processed by the firewall and before the inbound packet gets to the firewall.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Special NAT Handling ==&lt;br /&gt;
FTP is a dinosaur left over from the time before the Internet, when research universities were connected with leased lines and FTP was used to share files among research scientists. This was a time when data security was not even an idea yet. Over the years the FTP protocol became buried into the backbone of the emerging Internet and its login ID &amp;amp; PW being sent in clear text was never changed to address new security concerns. FTP has two flavors: it can run in active mode or passive mode. The difference is in how the data channel is acquired. Passive mode is more secure as the data channel is acquired be the ordinal FTP session requester. For a real good explanation of FTP and its different modes read  http://www.slacksite.com/other/ftp.html&lt;br /&gt;
&lt;br /&gt;
NAT has a special built in FTP proxy option which can be specified on the NAT map rule. It can monitor all outbound packet traffic for active or passive FTP start session requests and dynamically create temporary filter rules containing only the port number really in use for the data channel. This eliminates the security risk FTP normally exposes the firewall to from having large ranges of high order port numbers open. You specify the map rule like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 0.0.0.0/0 -&amp;gt; 0/32 proxy port 21 ftp/tcp&lt;br /&gt;
map dc0 10.0.10.0/29 -&amp;gt; 0/32&lt;br /&gt;
&lt;br /&gt;
The first rule handles all FTP traffic for the private LAN.&lt;br /&gt;
The second rule handles all FTP traffic from the gateway.&lt;br /&gt;
The third rule handles all non-FTP traffic for the private LAN.&lt;br /&gt;
All the non-FTP gateway traffic is using the public IP address by default so there is no ipnat rule needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The FTP map rule goes before our regular map rule. All packets are tested against the first rule from the top. First, it matches on interface name, then private LAN source IP address, and then if it's an FTP packet. If all that matches then the special FTP proxy creates temporary filter rules to let the FTP session packets pass in and out in addition to also NATing the FTP packets. ALL LAN packets that are not FTP do not match the first rule and fall through to the third rule and are tested, matching on interface and source IP, then get NATed. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FTP Filter rules ==&lt;br /&gt;
Only one filter rule is needed for FTP if NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes. &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Three Filter rules are needed for FTP if no NAT FTP proxy is used&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Allow out LAN PC client FTP to public Internet&lt;br /&gt;
# Active and passive modes.&lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out passive mode data channel high order port numbers &lt;br /&gt;
pass out quick on rl0 proto tcp from any to any port &amp;gt; 1024 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Active mode let data channel in from FTP server&lt;br /&gt;
pass in quick on rl0 proto tcp from any to any port = 20 flags S keep state&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:15:08Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
To build a custom kernel see the Kernel Customizing section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Inclusive Rule set Example ==&lt;br /&gt;
The following rule set is an example of how to code a very secure inclusive type of firewall. An inclusive firewall only allows services matching pass rules through and blocks all others by default. All firewalls have at the minimum two interfaces which have to have rules to allow the firewall to function.&lt;br /&gt;
&lt;br /&gt;
All Unix flavored systems including FBSD are designed to use interface lo0 and IP address 127.0.0.1 for internal communication within the FBSD operating system. The firewall rules must contain rules to allow free, unmolested movement of these special internally used packets.&lt;br /&gt;
&lt;br /&gt;
The interface which faces the public Internet is the one which you code your rules to authorize and control access out to the public Internet and access requests arriving from the public Internet. This can be your ‘user ppp’ tun0 interface or your NIC that is cabled to your DSL or cable modem. &lt;br /&gt;
&lt;br /&gt;
In cases where one or more NIC's are cabled to private LANs (local area networks) behind the firewall, those interfaces must have a rule coded to allow free, unmolested movement of packets originating from those LAN interfaces.&lt;br /&gt;
&lt;br /&gt;
The rules should be first organized into three major sections; all the free and unmolested interfaces, public interface outbound, and the public interface inbound. &lt;br /&gt;
&lt;br /&gt;
The order of the rules in each of the public interface sections should be in order of the most used rules being placed before less often used rules with the last rule in the section being a block log all packets on that interface and direction. &lt;br /&gt;
&lt;br /&gt;
The outbound section in the following rule set only contains ‘pass’ rules which contain selection values that uniquely identify the service that is authorized for public Internet access. All the rules have the ‘quick’, ‘on’, proto, port, and keep state option coded. The ‘proto tcp’ rules have the ‘flag’ option included to identify the session start request as the triggering packet to activate the stateful facility. &lt;br /&gt;
&lt;br /&gt;
The inbound section has all the blocking of undesirable packets first for two different reasons. First, these things being blocked may be part of an otherwise valid packet which may be allowed in by the later authorized service rules. Second, by having a rule that explicitly blocks selected packets that I receive on an infrequent bases and don’t want to see in the log, this keeps them from being caught by the last rule in the section which blocks and logs all packets which have fallen through the rules. The last rule in the section which blocks and logs all packets is how you create the legal evidence needed to prosecute the people who are attacking your system. &lt;br /&gt;
&lt;br /&gt;
Another thing you should take note of is there is no response returned for any of the undesirable stuff; their packets just get dropped and vanish. This way the attackers have no knowledge if their packets have reached your system. The less the attackers can learn about your system the more secure it is. For the inbound 'nmap OS fingerprint' attempts rule I log the first occurrence, because this is something an attacker would do.&lt;br /&gt;
&lt;br /&gt;
Any time you see log messages on a rule with ‘log first’ you should do an ipfstat –h command to see the number of times the rule has been matched so you know if you are being flooded, (IE: under attack). &lt;br /&gt;
&lt;br /&gt;
When you log packets with port numbers you do not recognize, go to http://www.securitystats.com/tools/portsearch.php and do a port number lookup to find what the purpose of that port number is. &lt;br /&gt;
&lt;br /&gt;
Check out this link for port numbers used by Trojans http://www.simovits.com/trojans/trojans.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following rule set is a complete, very secure ‘inclusive’ type of firewall rule set that I have used on my system. You cannot go wrong using this rule set for your own. Just comment out any pass rules for services to don’t want to authorize. &lt;br /&gt;
&lt;br /&gt;
If you see messages in your log that you want to stop seeing just add a block rule in the inbound section. &lt;br /&gt;
&lt;br /&gt;
You have to change the ‘dc0’ interface name in every rule to the interface name of the NIC that connects your system to the public Internet. For ‘user ppp’ it would be ‘tun0’.&lt;br /&gt;
&lt;br /&gt;
Add the following statements to /etc/ipf.rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Inside LAN Interface for private network&lt;br /&gt;
# Not needed unless you have LAN&lt;br /&gt;
################################################################# &lt;br /&gt;
&lt;br /&gt;
#pass out quick on xl0 all &lt;br /&gt;
#pass in quick on xl0 all&lt;br /&gt;
&lt;br /&gt;
################################################################# &lt;br /&gt;
# No restrictions on Loopback Interface &lt;br /&gt;
################################################################# &lt;br /&gt;
pass in quick on lo0 all &lt;br /&gt;
pass out quick on lo0 all &lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Outbound Section) &lt;br /&gt;
# Interrogate session start requests originating from behind the &lt;br /&gt;
# firewall on the private network &lt;br /&gt;
# or from this gateway server destined for the public Internet.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
# xxx must be the IP address of your ISP’s DNS.&lt;br /&gt;
# Dup these lines if your ISP has more than one DNS server&lt;br /&gt;
# Get the IP addresses from /etc/resolv.conf file&lt;br /&gt;
pass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto udp from any to xxx port = 53 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's DHCP server for cable or DSL networks.&lt;br /&gt;
# This rule is not needed for ‘user ppp’ type connection to the &lt;br /&gt;
# public Internet, so you can delete this whole group.&lt;br /&gt;
# Use the following rule and check log for IP address. &lt;br /&gt;
# Then put IP address in commented out rule &amp;amp; delete first rule &lt;br /&gt;
pass out log quick on dc0 proto udp from any to any port = 67 keep state&lt;br /&gt;
#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 443 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out send &amp;amp; get email function&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 110 flags S keep state&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 25 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out Time&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 37 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out nntp news&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 119 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out gateway &amp;amp; LAN users non-secure FTP ( both passive &amp;amp; active modes) &lt;br /&gt;
# This function uses the IPNAT built in FTP proxy function coded in &lt;br /&gt;
# the NAT rules file to make this single rule function correctly. &lt;br /&gt;
# If you want to use the pkg_add command to install application packages&lt;br /&gt;
# on your gateway system you need this rule. &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 21 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out secure FTP, Telnet, and SCP &lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure Telnet &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out FBSD CVSUP function &lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out ping to public Internet&lt;br /&gt;
pass out quick on dc0 proto icmp from any to any icmp-type 8 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow out whois for LAN PC to public Internet&lt;br /&gt;
pass out quick on dc0 proto tcp from any to any port = 43 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only the first occurrence of everything &lt;br /&gt;
# else that’s trying to get out.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block out log first quick on dc0 all&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
# Interface facing Public Internet (Inbound Section)&lt;br /&gt;
# Interrogate packets originating from the public Internet&lt;br /&gt;
# destined for this gateway server or the private network.&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# Block all inbound traffic from non-routable or reserved address spaces&lt;br /&gt;
block in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP&lt;br /&gt;
block in quick on dc0 from 127.0.0.0/8 to any       #loopback&lt;br /&gt;
block in quick on dc0 from 0.0.0.0/8 to any         #loopback&lt;br /&gt;
block in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config&lt;br /&gt;
block in quick on dc0 from 192.0.2.0/24 to any      #reserved for doc's&lt;br /&gt;
block in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect&lt;br /&gt;
block in quick on dc0 from 224.0.0.0/3 to any       #Class D &amp;amp; E multicast&lt;br /&gt;
&lt;br /&gt;
##### Block a bunch of different nasty things. ############&lt;br /&gt;
# That I don’t want to see in the log &lt;br /&gt;
&lt;br /&gt;
# Block frags&lt;br /&gt;
block in quick on dc0 all with frags&lt;br /&gt;
&lt;br /&gt;
# Block short tcp packets&lt;br /&gt;
block in quick on dc0 proto tcp all with short&lt;br /&gt;
&lt;br /&gt;
# block source routed packets&lt;br /&gt;
block in quick on dc0 all with opt lsrr&lt;br /&gt;
block in quick on dc0 all with opt ssrr&lt;br /&gt;
&lt;br /&gt;
# Block nmap OS fingerprint attempts&lt;br /&gt;
# Log first occurrence of these so I can get their IP address&lt;br /&gt;
block in log first quick on dc0 proto tcp from any to any flags FUP&lt;br /&gt;
&lt;br /&gt;
# Block anything with special options&lt;br /&gt;
block in quick on dc0 all with ipopts &lt;br /&gt;
&lt;br /&gt;
# Block public pings &lt;br /&gt;
block in quick on dc0 proto icmp all icmp-type 8&lt;br /&gt;
&lt;br /&gt;
# Block ident &lt;br /&gt;
block in quick on dc0 proto tcp from any to any port = 113&lt;br /&gt;
&lt;br /&gt;
# Block all Netbios service. 137=name, 138=datagram, 139=session &lt;br /&gt;
# Netbios is MS/Windows sharing services.&lt;br /&gt;
# Block MS/Windows hosts2 name server requests 81&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 137&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 138&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 139&lt;br /&gt;
block in log first quick on dc0 proto tcp/udp from any to any port = 81&lt;br /&gt;
&lt;br /&gt;
# Allow traffic in from ISP's DHCP server. This rule must contain&lt;br /&gt;
# the IP address of your ISP’s DHCP server as it’s the only &lt;br /&gt;
# authorized source to send this packet type. Only necessary for &lt;br /&gt;
# cable or DSL configurations. This rule is not needed for&lt;br /&gt;
# ‘user ppp’ type connection to the public Internet.&lt;br /&gt;
# This is the same IP address you captured and &lt;br /&gt;
# used in the outbound section.&lt;br /&gt;
pass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in standard www function because I have apache server&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 80 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in non-secure Telnet session from public Internet&lt;br /&gt;
# labeled non-secure because ID/PW passed over public Internet as clear text.&lt;br /&gt;
# Delete this sample group if you do not have telnet server enabled.&lt;br /&gt;
#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Allow in secure FTP, Telnet, and SCP from public Internet&lt;br /&gt;
# This function is using SSH (secure shell)&lt;br /&gt;
pass in quick on dc0 proto tcp from any to any port = 22 flags S keep state&lt;br /&gt;
&lt;br /&gt;
# Block and log only first occurrence of all remaining traffic &lt;br /&gt;
# coming into the firewall. The logging of only the first &lt;br /&gt;
# occurrence stops a ‘denial of service’ attack targeted&lt;br /&gt;
# at filling up your log file space.&lt;br /&gt;
# This rule enforces the block all by default logic. &lt;br /&gt;
block in log first quick on dc0 all&lt;br /&gt;
################### End of rules file ################################# &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:11:34Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* Rule Syntax */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
To build a custom kernel see the Kernel Customizing section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end &lt;br /&gt;
of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:09:01Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
To build a custom kernel see the Kernel Customizing section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF Rule Sets ==&lt;br /&gt;
A rule set is a group of IPF rules coded to pass or block packets based on the values contained in the packet. The bi-directional exchange of packets between hosts comprises a session conversation. The firewall rule set processes the packet two times, once on its arrival from the public Internet host and again as it leaves for its return trip back to the public Internet host. Each TCP/IP service (IE: telnet, www, mail, etc;) is predefined by its protocol, source and destination IP address, or the source and destination port number. This is the basic selection criteria used to create rules which will pass or block services. &lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this section are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public Internet and also control the services which can originate from the public Internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
Installers Note: Warning, when working with the firewall rules, always, always do it from the root console of the system running the firewall or you can end up locking yourself out.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Rule Syntax ==&lt;br /&gt;
The rule syntax presented here has been simplified to only address the modern stateful rule context and ‘first matching rule wins’ logic. For the complete legacy rule syntax description see the online ‘man ipf’ page at http://www.freebsd.org/cgi/man.cgi?query=ipf&amp;amp;apropos=0&amp;amp;sektion=0&amp;amp;manpath=FreeBSD+6.0-RELEASE+and+Ports&amp;amp;format=html&lt;br /&gt;
&lt;br /&gt;
# is used to mark the start of a comment and may appear at the end of a rule line or on its own line. Blank lines are ignored.&lt;br /&gt;
&lt;br /&gt;
Rules contain keywords. These keywords have to be coded in a specific order from left to right on the line. Keywords are identified in bold type. Some keywords have sub-options which may be keywords themselves and also include more sub-options. Each of the headings in the below syntax has a bold section header which expands on the content.&lt;br /&gt;
&lt;br /&gt;
Syntax = ACTION IN-OUT OPTIONS SELECTION STATEFUL&lt;br /&gt;
&lt;br /&gt;
ACTION = block | pass &lt;br /&gt;
&lt;br /&gt;
IN-OUT = in | out&lt;br /&gt;
&lt;br /&gt;
OPTIONS = log | quick | on interface-name &lt;br /&gt;
&lt;br /&gt;
SELECTION = proto value | source/destination IP | port = number | flags flag-value &lt;br /&gt;
&lt;br /&gt;
Where value = tcp/udp | udp | tcp | icmp &lt;br /&gt;
&lt;br /&gt;
Where source/destination IP = all | from object to object &lt;br /&gt;
&lt;br /&gt;
Where object = IP address | any&lt;br /&gt;
&lt;br /&gt;
Where number = port number&lt;br /&gt;
&lt;br /&gt;
Where flag-value = S &lt;br /&gt;
&lt;br /&gt;
STATEFUL = keep state&lt;br /&gt;
&lt;br /&gt;
The | symbol used in the above syntax means ‘or’. &lt;br /&gt;
&lt;br /&gt;
ACTION&lt;br /&gt;
&lt;br /&gt;
The action indicates what to do with the packet if it matches the rest of the filter rule. Each rule MUST have an action. The following actions are recognized:&lt;br /&gt;
&lt;br /&gt;
block indicates that the packet should be dropped if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
pass indicates that the packet should exit the firewall if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
IN-OUT It is a mandatory requirement that each filter rule explicitly states which side of the I/O it is to be used on. The next keyword must be either in or out and one or the other has to be coded or the rule will not pass syntax check. &lt;br /&gt;
&lt;br /&gt;
in means this rule is being applied against an inbound packet which has just been received on the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
out means this rule is being applied against an outbound packet destined for the interface facing the public Internet. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
OPTIONS Must be used in the order shown here. &lt;br /&gt;
&lt;br /&gt;
log indicates that the packet header will be written to the ipl log (as described in the LOGGING section below) if the selection parameters match the packet. &lt;br /&gt;
&lt;br /&gt;
quick indicates that if the selection parameters match the packet, this rule will be the last rule checked, allowing a &amp;quot;short-circuit&amp;quot; path to avoid processing any following rules for this packet. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
on indicates the interface name to be incorporated into the selection parameters. Interface names are as displayed by ifconfig. Using this option, the rule will only match if the packet is going through that interface in the specified direction (in/out). This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When a packet is logged, the headers of the packet are written to the IPL packet logging pseudo-device. Immediately following the log keyword, the following qualifiers may be used (in this order):&lt;br /&gt;
&lt;br /&gt;
body indicates that the first 128 bytes of the packet contents will be logged after the headers.&lt;br /&gt;
&lt;br /&gt;
first If the ‘log’ keyword is being used in conjunction with a &amp;quot;keep state&amp;quot; option, it is recommended that this option is also applied so that only the triggering packet is logged and not every packet thereafter which matches the ‘keep state’ information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTION &lt;br /&gt;
&lt;br /&gt;
The keywords described in this section are used to describe attributes of the packet to be interrogated when determining whether rules match or don't match. There is a keyword subject, and it has sub-option keywords, one of which has to be selected. The following general-purpose attributes are provided for matching and must be used in this order:&lt;br /&gt;
&lt;br /&gt;
proto value Proto is the subject keyword; it must be coded along with one of it’s corresponding keyword sub-option values. The value allows a specific protocol to be matched against it. This option is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
Valid sub-option value keywords are:&lt;br /&gt;
&lt;br /&gt;
tcp/udp | udp | tcp | icmp or any protocol names found in /etc/protocols are recognized and may be used. The special protocol keyword tcp/udp may be used to match either a TCP or a UDP packet, and has been added as a convenience to save duplication of otherwise identical rules.&lt;br /&gt;
&lt;br /&gt;
source/destination IP = &lt;br /&gt;
&lt;br /&gt;
all keyword is essentially a synonym for &amp;quot;from any to any&amp;quot; with no other match parameters.&lt;br /&gt;
&lt;br /&gt;
from src to dst The from and to keywords are used to match against IP addresses. Rules must specify BOTH source and destination parameters. ‘any’ is a special keyword that matches any IP address. As in &amp;quot;from any to any&amp;quot; or &lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0/0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0/0 &amp;quot; or&lt;br /&gt;
&lt;br /&gt;
&amp;quot;from 0.0.0.0 to any&amp;quot; or &amp;quot;from any to 0.0.0.0 &amp;quot; &lt;br /&gt;
&lt;br /&gt;
IP addresses may be specified as a dotted IP address numeric form/mask-length, or as a single dotted IP address numeric form. &lt;br /&gt;
&lt;br /&gt;
There isn't a way to match ranges of IP addresses which do not express themselves easily as mask-length. See this link for help on writing mask-length. http://jodies.de/ipcalc&lt;br /&gt;
&lt;br /&gt;
port If a port match is included, for either or both of the source and destination, then it is only applied to TCP and UDP packets. When composing port comparisons, either the service name from /etc/services or an integer port number may be used. When the port appears as part of the from object, it matches the source port number; when it appears as part of the to object, it matches the destination port number. The use of the port option with the ‘to’ object is a mandatory requirement for the modernized rules processing logic. &lt;br /&gt;
&lt;br /&gt;
As in ‘from any to any port = 80’&lt;br /&gt;
&lt;br /&gt;
Port comparisons may be done in a number of forms, with a number of comparison operators, or port ranges may be specified.&lt;br /&gt;
&lt;br /&gt;
port &amp;quot;=&amp;quot; | &amp;quot;!=&amp;quot; | &amp;quot;&amp;lt;&amp;quot; | &amp;quot;&amp;gt;&amp;quot; | &amp;quot;&amp;lt;=&amp;quot; | &amp;quot;&amp;gt;=&amp;quot; | &amp;quot;eq&amp;quot; | &amp;quot;ne&amp;quot; | &amp;quot;lt&amp;quot; | &amp;quot;gt&amp;quot; | &amp;quot;le&amp;quot; | &amp;quot;ge&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
To specify port ranges, port &amp;quot;&amp;lt;&amp;gt;&amp;quot; | &amp;quot;&amp;gt;&amp;lt;&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following the source and destination matching parameters, the following two parameters are mandatory requirements for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
flags is only effective for TCP filtering. The letters represents one of the possible flags that can be interrogated in the TCP packet header. &lt;br /&gt;
&lt;br /&gt;
The modernized rules processing logic uses the ‘flags S’ parameter to identify the tcp session start request.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
STATEFUL &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keep state indicates that on a pass rule, any packets that match the rules selection parameters are to activate the stateful filtering facility. &lt;br /&gt;
&lt;br /&gt;
This option is a mandatory requirement for the modernized rules processing logic.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stateful Filtering ==&lt;br /&gt;
Stateful filtering treats traffic as a bi-directional exchange of packets comprising a session conversation. When activated keep-state dynamically generates internal rules for each anticipated packet being exchanged during the bi-directional session conversation. It has the interrogation abilities to determine if the session conversation between the originating sender and the destination are following the valid procedure of bi-directional packet exchange. Any packets that do not properly fit the session conversation template are automatically rejected as impostors.&lt;br /&gt;
&lt;br /&gt;
Keep state will also allow ICMP packets related to a TCP or UDP session through. So if you get ICMP type 3 code 4 in response to some web surfing allowed out by a keep state rule, they will be automatically allowed in. Any packet that IPF can be certain is part of an active session, even if it's a different protocol, will be let in.&lt;br /&gt;
&lt;br /&gt;
What happens is:&lt;br /&gt;
&lt;br /&gt;
Packets destined to go out through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the outbound rule set. &lt;br /&gt;
&lt;br /&gt;
Packets coming in through the NIC connected to the public Internet are first checked against the dynamic state table. If the packet matches the next expected packet of that active session conversation, then it exits the firewall and the state of the session conversation flow is updated in the dynamic state table. The remaining packets get checked against the inbound rule set.&lt;br /&gt;
&lt;br /&gt;
When the conversation completes, it's removed from the dynamic state table. &lt;br /&gt;
&lt;br /&gt;
Stateful filtering allows you to focus on blocking/passing new sessions. If the new session is passed, all its subsequent packets will be allowed through automatically and any impostors automatically rejected. If a new session is blocked, none of its subsequent packets will be allowed through. Stateful filtering has technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T21:02:49Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
To build a custom kernel see the Kernel Customizing section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPMON Command ==&lt;br /&gt;
In order for ipmon to properly work, the kernel option IPFILTER_LOG must be turned on. This command has two different modes it can be used in. Native mode is the default mode when you type the command on the FBSD console command line without the –D flag. &lt;br /&gt;
&lt;br /&gt;
Daemon mode is for when you want to have a continuous system log file available so you can review logging of past events. This is how FBSD and IPFILTER are configured to work together. FBSD has a built in facility to automatically rotate syslogs. That is why outputting the log information to syslogd is better than the default of a regular file. In the rc.conf file you see the ipmon_flags statement uses the &amp;quot;-Ds&amp;quot; flags: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot; # D = start as daemon&lt;br /&gt;
                  # s = log to syslog&lt;br /&gt;
                  # v = log tcp window, ack, seq&lt;br /&gt;
                  # n = map IP &amp;amp; port to names&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefits of logging are obvious. Logging provides the ability to review after the fact information like what packets have been dropped, what addresses they came from, and where they were going, giving you a significant edge in tracking down attackers.&lt;br /&gt;
&lt;br /&gt;
Even with the logging facility enabled, IPF will not generate any rule logging on its own. The firewall administrator decides what rules in the rule set he wants to log and adds the log keyword to those rules. Normally only deny rules are logged. &lt;br /&gt;
&lt;br /&gt;
It’s very customary to include a default deny everything rule with the log keyword included as your last rule in the rule set. This way you get to see all the packets that did not match any of the rules in the rule set.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== IPMON Logging ==&lt;br /&gt;
Syslogd uses its own special method for segregation of log data. It uses special groupings called ‘facility’ and ‘level’. IPMON in –Ds mode uses local0 as the ‘facility’ name. All IPMON logged data goes to local0. &lt;br /&gt;
&lt;br /&gt;
You have to manually configure the /etc/syslog.conf file by adding the statements to direct the Local0 'facility' to the log file name recording the log records. FBSD keeps all of its syslog files in /var/log/ directory.&lt;br /&gt;
&lt;br /&gt;
First allocate the new named log file for the IPFMON logged data.&lt;br /&gt;
&lt;br /&gt;
touch /var/log/ipfilter.log     # will allocate the file&lt;br /&gt;
&lt;br /&gt;
The syslog function is controlled by definition statements in the /etc/syslog.conf file. &lt;br /&gt;
&lt;br /&gt;
You will have to edit the /etc/syslog.conf file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add the following statement to syslog.conf: &lt;br /&gt;
&lt;br /&gt;
local0.*     /var/log/ipfilter.log&lt;br /&gt;
&lt;br /&gt;
The local0.* means to write all the logged messages to the coded file location. &lt;br /&gt;
&lt;br /&gt;
To activate the changes to /etc/syslog.conf you can reboot or force the syslogd task into re-reading /etc/syslog.conf by issuing this console command&lt;br /&gt;
/etc/rc.d/syslogd reload &lt;br /&gt;
&lt;br /&gt;
Don’t forget to change /etc/newsyslog.conf to rotate the new named IPFILTER log you just created above. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Format of Logged Messages ==&lt;br /&gt;
Fields common to all messages are:&lt;br /&gt;
&lt;br /&gt;
1. The date of packet receipt. &lt;br /&gt;
&lt;br /&gt;
2. The time of packet receipt. This is in the form HH:MM:SS.F, for hours, minutes, seconds, and fractions of a second (which can be several digits long).&lt;br /&gt;
&lt;br /&gt;
3. The name of the interface the packet was processed on, e.g., dc0.&lt;br /&gt;
&lt;br /&gt;
4. The group and rule number of the rule, e.g., @0:17. &lt;br /&gt;
&lt;br /&gt;
These can be viewed with ipfstat -in.&lt;br /&gt;
&lt;br /&gt;
5. The action: p for passed, b for blocked, S for a short packet, n did not match any rules, L for a log rule. The order of precedence in showing flags is: &lt;br /&gt;
&lt;br /&gt;
S, p, b, n, L. A capital P or B means that the packet has been logged due to a global logging setting, not a particular rule.&lt;br /&gt;
&lt;br /&gt;
6. The addresses. This is actually three fields: the source address and port (separated by a comma), the -&amp;gt; symbol, and the destination address and port. 209.53.17.22,80 -&amp;gt; 198.73.220.17,1722.&lt;br /&gt;
&lt;br /&gt;
7. PR followed by the protocol name or number, e.g., PR tcp.&lt;br /&gt;
&lt;br /&gt;
8. len followed by the header length and total length of the packet,&lt;br /&gt;
&lt;br /&gt;
e.g., len 20 40.&lt;br /&gt;
&lt;br /&gt;
If the packet is a TCP packet, there will be an additional field starting with a hyphen followed by letters corresponding to any flags that were set. See the ipf.conf manual page for a list of letters and their flags.&lt;br /&gt;
&lt;br /&gt;
If the packet is an ICMP packet, there will be two fields at the end, the first always being `ICMP' and the next being the ICMP message and sub-message type, separated by a slash, (e.g., ICMP 3/3 for a port unreachable message).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Building Rule Script ==&lt;br /&gt;
Some experienced IPF users create a file containing the rules and code them in a manner compatible with running them as a script with symbolic substitution. The major benefit of doing this is you only have to change the value associated with the symbolic name, and when the script is run all the rules containing the symbolic name will have the value substituted in the rules. Being a script, you can use symbolic substitution to code frequently used values and substitute them in multiple rules. You will see this in the following example.&lt;br /&gt;
&lt;br /&gt;
The script syntax used here is compatible with the ‘sh’, ‘csh’, and ‘tcsh’ shells.&lt;br /&gt;
&lt;br /&gt;
Symbolic substitution fields are prefixed with a dollar sign $.&lt;br /&gt;
&lt;br /&gt;
Symbolic fields do not have the $ prefix&lt;br /&gt;
&lt;br /&gt;
The value to populate the Symbolic field must be enclosed with &amp;quot;double quotes&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Start your rules file with this. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
############# Start of IPF rules script ########################&lt;br /&gt;
&lt;br /&gt;
oif=&amp;quot;dc0&amp;quot;            # name of the outbound interface&lt;br /&gt;
odns=&amp;quot;192.0.2.11&amp;quot;    # ISP's dns server IP address Symbolic&amp;gt;&lt;br /&gt;
myip=&amp;quot;192.0.2.7&amp;quot;     # My Static IP address from ISP&lt;br /&gt;
ks=&amp;quot;keep state&amp;quot;&lt;br /&gt;
fks=&amp;quot;flags S keep state&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# You can use this same to build the /etc/ipf.rules file&lt;br /&gt;
#cat &amp;gt;&amp;gt; /etc/ipf.rules &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# exec ipf command and read inline data, stop reading &lt;br /&gt;
# when word EOF is found. There has to be one line&lt;br /&gt;
# after the EOF line to work correctly.&lt;br /&gt;
/sbin/ipf -Fa -f - &amp;lt;&amp;lt; EOF&lt;br /&gt;
&lt;br /&gt;
# Allow out access to my ISP's Domain name server.&lt;br /&gt;
pass out quick on $oif proto tcp from any to $odns port = 53 $fks&lt;br /&gt;
pass out quick on $oif proto udp from any to $odns port = 53 $ks&lt;br /&gt;
&lt;br /&gt;
# Allow out non-secure standard www function&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 80 $fks&lt;br /&gt;
&lt;br /&gt;
# Allow out secure www function https over TLS SSL&lt;br /&gt;
pass out quick on $oif proto tcp from $myip to any port = 443 $fks&lt;br /&gt;
EOF&lt;br /&gt;
################## End of IPF rules script ########################&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s all there is to it. The rules are not important in this example; how the Symbolic substitution field are populated and used are. If the above example was in the /etc/ipf.rules.script file, I could reload these rules by entering on the FBSD command line: &lt;br /&gt;
&lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&lt;br /&gt;
There is one problem with using a rules file with embedded symbolics. IPF has no problem with it, but the rc.conf &lt;br /&gt;
&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot; &lt;br /&gt;
&lt;br /&gt;
statement will not load the rules if the file this statement is pointing at contains symbolics. This is a FBSD rc.conf launch problem. &lt;br /&gt;
&lt;br /&gt;
The solution is to delete the following statement in the rc.conf&lt;br /&gt;
&lt;br /&gt;
ipfilter_rules= &lt;br /&gt;
&lt;br /&gt;
and put the following script in this directory:&lt;br /&gt;
&lt;br /&gt;
/usr/local/etc/rc.d/ &lt;br /&gt;
&lt;br /&gt;
FBSD looks in this directory for scripts that have names ending in ‘.sh’ to automatically launch during the boot process. Apache and DHCP place their launch scripts there. &lt;br /&gt;
&lt;br /&gt;
Your launch script should look like this.&lt;br /&gt;
&lt;br /&gt;
ee /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh &lt;br /&gt;
sh /etc/ipf.rules.script&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The permission on this script file must be read, write, exec for owner root.&lt;br /&gt;
&lt;br /&gt;
chmod 700 /usr/local/etc/rc.d/ipf.loadrules.sh&lt;br /&gt;
&lt;br /&gt;
Now when you system boots your IPF rules will be loaded using the script.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T20:55:04Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* IPFSTAT Command */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
To build a custom kernel see the Kernel Customizing section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T20:54:09Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* IPF COMMAND */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
To build a custom kernel see the Kernel Customizing section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T20:53:12Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: /* RC.CONF Options */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
To build a custom kernel see the Kernel Customizing section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall</id>
		<title>IPFILTER (IPF) Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/IPFILTER_(IPF)_Firewall"/>
				<updated>2012-08-13T20:49:22Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: IPFILTER (IPF) Firewall&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPFILTER (IPF) Firewall ==&lt;br /&gt;
The author of IPFILTER is Darren Reed. IPFILTER is not FBSD operating system dependant. IPFILTER is an open source application and has been ported to FreeBSD, NetBSD, OpenBSD, Sun, HP, and Solaris operating systems. IPFILTER is actively being supported and maintained, with updated versions being released regularly.&lt;br /&gt;
&lt;br /&gt;
The IPFILTER program runs in the kernel and consists of the firewall and separate NAT facilities. IPFILTER also has user-land front-end interactive interfaces for controlling the firewall rules, NAT, packet accounting, and the logging facility. Program IPF is used to load the firewall rules. Program IPNAT is used to load the firewall NAT rules. Program IPFSTAT reports on packet filter statistics and lists active rules sets. Program IPMON monitors IPFILTER for logged packets. &lt;br /&gt;
&lt;br /&gt;
From this point on IPFILTER will be written as IPF and is intended to mean the same thing as IPFILTER.&lt;br /&gt;
&lt;br /&gt;
IPF was originally written using a rules processing logic of ‘the last matching rule wins’ and used only stateless types of rules. Over time IPF has been enhanced to include a ‘quick’ option and a stateful ‘keep state’ option which drastically modernized the rules processing logic. IPF’s official documentation covers the legacy rule coding parameters and the legacy rule file processing logic, the modernized functions are only included as additional options, completely understating their benefits in producing a far superior secure firewall. &lt;br /&gt;
&lt;br /&gt;
The instructions contained in this guide are based on using rules that contain the ‘quick' option and the stateful ‘keep state’ option. This is the basic framework for coding an inclusive firewall rule set.&lt;br /&gt;
&lt;br /&gt;
An inclusive firewall only allows services matching the rules through. This way you can control what services can originate behind the firewall destined for the public internet and also control the services which can originate from the public internet accessing your private network. Everything else is blocked and logged by default design. Inclusive firewall rule sets are much more secure than exclusive firewall rule sets and are the only rule set type covered herein. &lt;br /&gt;
&lt;br /&gt;
For detailed explanation of the legacy rules processing method, see http://www.obfuscation.org/ipf/ipf-howto.html#TOC_1&lt;br /&gt;
&lt;br /&gt;
http://coombs.anu.edu.au/~avalon/ip-filter.html&lt;br /&gt;
&lt;br /&gt;
To see the FAQ: http://www.phildev.net/ipf/index.html&lt;br /&gt;
&lt;br /&gt;
To search the open source IPFilter questions archives: http://marc.theaimsgroup.com/?l=ipfilter&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Enabling IPF ==&lt;br /&gt;
IPF is included in the basic FBSD install as a separate run time loadable module. IPF will dynamically load its kernel loadable module when the rc.conf statement ipfilter_enable=&amp;quot;YES&amp;quot; is used. The loadable module was created with logging enabled and the ‘default pass all’ options. You do not need to compile IPF into the FBSD kernel just to change the default to ‘block all’; you can do that by just coding a block all rule at the end of your rule set. &lt;br /&gt;
&lt;br /&gt;
Using the IPF run time loadable module is recommended.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Kernel options ==&lt;br /&gt;
It is not a mandatory requirement that you enable IPF by compiling the following options into the FBSD kernel. It’s only presented here as background information. Compiling IPF into the kernel causes the loadable module to never be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sample kernel source IPF options statements are in the /usr/src/sys/i386/conf/LINT kernel source and are reproduced here.&lt;br /&gt;
&lt;br /&gt;
options IPFILTER &lt;br /&gt;
options IPFILTER_LOG &lt;br /&gt;
options IPFILTER_DEFAULT_BLOCK &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IPFILTER This tells the compile to include IPFILTER as part of it’s core kernel. &lt;br /&gt;
&lt;br /&gt;
IPFILTER_LOG enables the option to have IPF log traffic by writing to the ipl packet logging pseudo-device for every rule that has the &amp;quot;log&amp;quot; keyword.&lt;br /&gt;
&lt;br /&gt;
IPFILTER_DEFAULT_BLOCK This option changes the default behavior so any packet not matching a firewall ‘pass’ rule gets blocked.&lt;br /&gt;
&lt;br /&gt;
To build a custom kernel see the Kernel Customizing section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== RC.CONF Options ==&lt;br /&gt;
You need the follow statements in /etc/rc.conf to activate IPF at boot time. &lt;br /&gt;
&lt;br /&gt;
ipfilter_enable=&amp;quot;YES&amp;quot;             # Start ipf firewall&lt;br /&gt;
ipfilter_rules=&amp;quot;/etc/ipf.rules&amp;quot;   # loads rules definition text file&lt;br /&gt;
                                  # IE: not script file with rules in it &lt;br /&gt;
&lt;br /&gt;
ipmon_enable=&amp;quot;YES&amp;quot;                # Start IP monitor log&lt;br /&gt;
ipmon_flags=&amp;quot;-Ds&amp;quot;                 # D = start as daemon&lt;br /&gt;
                                  # s = log to syslog&lt;br /&gt;
                                  # v = log tcp window, ack, seq &lt;br /&gt;
                                  # n = map IP &amp;amp; port to names&lt;br /&gt;
                                  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have a LAN behind this firewall that uses the reserved private IP address ranges, then you need to add the following to enable NAT functionality.&lt;br /&gt;
&lt;br /&gt;
gateway_enable=&amp;quot;YES&amp;quot;              # Enable as LAN gateway&lt;br /&gt;
ipnat_enable=&amp;quot;YES&amp;quot;                # Start ipnat function&lt;br /&gt;
ipnat_rules=&amp;quot;/etc/ipnat.rules&amp;quot;    # rules definition file for ipnat&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPF COMMAND ==&lt;br /&gt;
The ipf command is used to load your rules file. Normally you create a file containing your custom rules and use this command to replace in mass the currently running firewall internal rules. &lt;br /&gt;
&lt;br /&gt;
ipf –Fa –f /etc/ipf.rules&lt;br /&gt;
&lt;br /&gt;
-Fa means flush all internal rules tables&lt;br /&gt;
&lt;br /&gt;
-f means this is the file to read for the rules to load &lt;br /&gt;
&lt;br /&gt;
This gives the user the ability to make changes to their custom rules file and run the above IPF command, thus updating the running firewall with a fresh copy of all the rules without having to reboot the system. This method is very convenient for testing new rules as the procedure can be executed as many times as needed.&lt;br /&gt;
&lt;br /&gt;
See man IPF(8) for details on the other flag options available with this command.&lt;br /&gt;
&lt;br /&gt;
The ipf command expects the rules file to be a standard text file. It will not accept a rules file written as a script with symbolic substitution. &lt;br /&gt;
&lt;br /&gt;
There is a way to build IPF rules that utilities the power of script symbolic substitution. See the Building Rule Script section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IPFSTAT Command ==&lt;br /&gt;
The default behavior of ipfstat is to retrieve and display the totals of the accumulated statistics gathered as a result of applying the user coded rules against packets going in and out of the firewall since it was last started or since the last time the accumulators were reset to zero by the ipf –Z command. &lt;br /&gt;
&lt;br /&gt;
See ‘man ipfstat’ for details.&lt;br /&gt;
&lt;br /&gt;
This is what the ipfstat command displays without any flags:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
input packets: blocked 99286 passed 1255609 nomatch 14686 counted 0&lt;br /&gt;
output packets: blocked 4200 passed 1284345 nomatch 14687 counted 0&lt;br /&gt;
input packets logged: blocked 99286 passed 0&lt;br /&gt;
output packets logged: blocked 0 passed 0&lt;br /&gt;
packets logged: input 0 output 0&lt;br /&gt;
log failures: input 3898 output 0&lt;br /&gt;
fragment state(in): kept 0 lost 0&lt;br /&gt;
fragment state(out): kept 0 lost 0&lt;br /&gt;
packet state(in): kept 169364 lost 0&lt;br /&gt;
packet state(out): kept 431395 lost 0&lt;br /&gt;
ICMP replies: 0 TCP RSTs sent: 0&lt;br /&gt;
Result cache hits(in): 1215208 (out): 1098963&lt;br /&gt;
IN Pullups succeeded: 2 failed: 0&lt;br /&gt;
OUT Pullups succeeded: 0 failed: 0&lt;br /&gt;
Fastroute successes: 0 failures: 0&lt;br /&gt;
TCP cksum fails(in): 0 (out): 0&lt;br /&gt;
Packet log flags set: (0)&lt;br /&gt;
&lt;br /&gt;
When supplied with either -i for inbound or –o for outbound, it will retrieve and display the appropriate list of filter rules currently installed and in use by the kernel.&lt;br /&gt;
&lt;br /&gt;
Ipfstat –in displays the inbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Ipfstat –on displays the outbound internal rules table with rule numbers&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
@1 pass out on xl0 from any to any&lt;br /&gt;
@2 block out on dc0 from any to any&lt;br /&gt;
@3 pass out quick on dc0 proto tcp/udp from any to any keep state&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –ih displays the inbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Ipfstat –oh displays the outbound internal rules table, each rule prefixed with count of times the rule was matched&lt;br /&gt;
&lt;br /&gt;
Rules will be displayed like this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2451423 pass out on xl0 from any to any&lt;br /&gt;
&lt;br /&gt;
354727 block out on dc0 from any to any&lt;br /&gt;
430918 pass out quick on dc0 proto tcp/udp from any to any keep state &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Ipfstat –t [ -C | -D | -P | -S | -T ]&lt;br /&gt;
&lt;br /&gt;
The most important function of the ipfstat command is the –t flag which activates the display state table in a way similar to the way the ‘top’ command shows the FBSD running process table. When your firewall is under attack this function gives you the ability to identify, drill down to, and see the attacking packets. The optional sub-flags give the ability to select destination IP and port, or source IP and port, or protocol that you want to monitor in real time. See man ipfstat for details.&lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category:FreeBSD for Servers]]&lt;br /&gt;
[[Category:Configuring FreeBSD]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Firewall</id>
		<title>Firewall</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Firewall"/>
				<updated>2012-08-13T20:35:45Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Firewalls ==&lt;br /&gt;
All software firewall applications are based on monitoring network packet traffic flow to and from your system. The values of selected packet control fields can be interrogated by user written rules to allow or deny packet traffic based on your security needs. &lt;br /&gt;
&lt;br /&gt;
Selection can be based on source and destination IP address, the source and destination port number, the type of protocol used (TCP, UDP, ICMP), or any combination. Firewall software applications provide a much, much finer level of control than that provided by a hardware router. They can be used to protect a single FBSD system or a complete internal network (LAN) by preventing public Internet traffic from making arbitrary connections to your internal network. They may also be used to prevent public Internet entities from spoofing internal IP addresses and to disable services you do not want accessed from the public Internet or by internal LAN users.&lt;br /&gt;
&lt;br /&gt;
Finally, firewalls may be used to support NAT (network address translation), which allows an internal network using private IP addresses to share a single connection to the public Internet, or letting commercial users share a range of static public IP addresses automatically among the LAN users.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Firewall Rule Set Types ==&lt;br /&gt;
Constructing a software application firewall rule set may seem to be trivial, but most people get it wrong. The most common mistake is to create an exclusive firewall rather than an inclusive firewall. &lt;br /&gt;
&lt;br /&gt;
An exclusive firewall allows all services through except for those matching a set of rules that block certain services. &lt;br /&gt;
&lt;br /&gt;
An inclusive firewall does the reverse. It only allows services matching the rules through and blocks everything else. This way you can control what services can originate behind the firewall destined for the public Internet and also control which services originating from the public Internet may access your network. Inclusive firewalls are much, much safer than exclusive firewalls. &lt;br /&gt;
&lt;br /&gt;
When you use your browser to access a web site there are many internal functions that happen before your screen fills with the data from the target web site. Your browser does not receive one large file containing all the data and display format instructions at one time. Each internal function accesses the public Internet in multiple send/receive cycles of packets of information. When all the packets containing the data finally arrive, the data contained in the packets is combined together to fill your screen. Each service has its own port number. The port number 80 is for web page services. So you can code your firewall to only allow web page session start requests originating from your LAN to pass through the firewall out to the public Internet. &lt;br /&gt;
&lt;br /&gt;
Security can be tightened further by telling the firewall to monitor the send/receive cycles of all the packets making up that session until the session  completes. These are called stateful capabilities and provide the maximum level of protection. &lt;br /&gt;
&lt;br /&gt;
A firewall rule set that does not implement stateful capabilities on all the services being authorized is an insecure firewall that is still open to many of the most common methods of attack. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Firewall Software Applications ==&lt;br /&gt;
FBSD has three different firewall software products built into the base system. They are IPFILTER also known as IPF, IPFIREWALL also known as IPFW, and the OpenBSD Packet Filter known as PF. IPFW has the built in traffic shaper facilities for controlling bandwidth usage called dummynet. PF has it's built in traffic shaper facilities for controlling bandwidth usage called ALTQ. IPFILTER does not have a built in traffic shaper facility for controlling bandwidth usage, but the ALTQ port application can be used to accomplish the same function. The dummynet feature and ALTQ is generally useful only to large ISPs or commercial users. IPF, IPFW, and IP use rules to control the access of packets to and from your system, although they go about it different ways and have different rule syntaxes. &lt;br /&gt;
&lt;br /&gt;
The IPFW /etc/rc.firewall sample rule set delivered in the basic install is outdated, complicated and does not use stateful rules on the interface facing the public Internet. It exclusively uses legacy stateless rules which only have the ability to open or close the service ports. The IPFW example stateful rule sets presented here supercedes the /etc/rc.firewall file distributed with the system.  &lt;br /&gt;
&lt;br /&gt;
Stateful rules have technically advanced interrogation abilities capable of defending against the flood of different attack methods currently employed by attackers.&lt;br /&gt;
&lt;br /&gt;
Both of these firewall software solutions IPF and IPFW still maintain the legacy heritage of their original rule processing order and reliance on non-stateful rules. These outdated concepts are not covered here, only the new, modern stateful rule construct and rule processing order is presented. &lt;br /&gt;
&lt;br /&gt;
You should read about all 3 firewalls, and them make your own decision on which one best fits your needs. &lt;br /&gt;
&lt;br /&gt;
The author prefers IPFILTER because its stateful rules are much less complicated to use in a Nat environment, and it has a built in FTP proxy that simplifies the rules to allow secure outbound FTP usage. It is also more appropriate to the knowledge level of the inexperienced firewall user. &lt;br /&gt;
&lt;br /&gt;
Since all firewalls are based on interrogating the values of selected packet control fields, the creator of the firewall rules must have an understanding of how TCP/IP works, what the different values in the packet control fields are and how these values are used in a normal session conversation. For a good explanation go to http://www.ipprimer.com/overview.cfm.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
[[Category:Securing FreeBSD]]&lt;br /&gt;
[[Category: FreeBSD Terminology]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Burncd</id>
		<title>Burncd</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Burncd"/>
				<updated>2012-08-13T20:03:57Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
NOTE: In Release 9.0 and newer the burncd utility is obsolete and does not function any more. You have to use cdrtools system utilites for writing cd and dvd media.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
pkg_add -r cdrtools&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:FreeBSD for Workstations]]&lt;br /&gt;
[[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	<entry>
		<id>http://freebsdwiki.net/index.php/Installing_fonts</id>
		<title>Installing fonts</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php/Installing_fonts"/>
				<updated>2012-08-13T20:01:07Z</updated>
		
		<summary type="html">&lt;p&gt;173.88.199.104: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Install Xfstt through packages&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
shell# pkg_add -r xfstt&lt;br /&gt;
&lt;br /&gt;
Follow the instructions after xfstt is installed&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
You will have to install your TrueType fonts in&lt;br /&gt;
&lt;br /&gt;
        PREFIX/lib/X11/fonts/TrueType&lt;br /&gt;
&lt;br /&gt;
Remember to run the &amp;quot;xfstt --sync&amp;quot; command if you&lt;br /&gt;
add fonts while the font server is running.&lt;br /&gt;
&lt;br /&gt;
You should also add the following line to the &amp;quot;Files&amp;quot;&lt;br /&gt;
Section of /etc/XF86Config:&lt;br /&gt;
&lt;br /&gt;
        FontPath   &amp;quot;unix/:7101&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Under FreeBSD 5.4 I installed my TrueType fonts in:&lt;br /&gt;
/usr/X11R6/lib/X11/fonts/TrueType&lt;br /&gt;
&lt;br /&gt;
shell# xfstt --sync&lt;br /&gt;
&lt;br /&gt;
Now you have to restart X&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== For those who have X.Org ==&lt;br /&gt;
&lt;br /&gt;
One can install Truetype fonts just by installing any of the truetype ports in the $PREFXports/x11-fonts&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# cd /usr/ports/x11-fonts &amp;amp;&amp;amp; make search key=truetype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Xorg can handle the fonts directly and therefore doesn't require you to use Xfstt.&lt;br /&gt;
&lt;br /&gt;
[[Category:Common Tasks]]&lt;/div&gt;</summary>
		<author><name>173.88.199.104</name></author>	</entry>

	</feed>