IPFW(8)

NAME
ipfw - controlling utility for IP firewall

SYNOPSIS
ipfw file
ipfw flush
ipfw zero [ number]
ipfw delete number
ipfw [ -atN] list
ipfw add [ number] action [ log] proto from src to dst [ via name|ipno] [ options]

DESCRIPTION
If used as shown in the first synopsis line, the file will be read line by line and applied as arguments to the ipfw command.

The ipfw code works by going through the rule-list for each packet, until a match is found. All rules have two counters associated with them, a packet count and a byte count. These counters are updated when a packet matches the rule.

The rules are ordered by a ``line-number'' that is used to order and delete rules. If a rule is added without a number, it is put at the end, just before the terminal ``policy-rule'', and numbered 100 higher than the previous rule.

One rule is always present:

    65535 deny all from any to any

this rule is the default policy, ie. don't allow anything at all. Your job in setting up rules is to modify this policy to match your needs.

The following options are available:

-a  While listing, show counter values. This option is the only way to see accounting records.

-t  While listing, show last match timestamp.

-N  Try to resolve addresses and service names.

action:

allow
    Allow packets that match rule. The search terminates.

pass
    Same as allow.

accept
    Same as allow.

count
    Update counters for all packets that match rule. The search continues with next rule.

deny
    Discard packets that match this rule. The search terminates.

reject
    Discard packets that match this rule, try to send ICMP notice. The search terminates.

When a packet matches a rule with the log keyword, a message will be printed on the console. If the kernel was compiled with the IP_FIREWALL_VERBOSE_LIMIT option, then logging will cease after the number of packets specified by the option are received for that particular chain entry. Logging may then be re-enabled by clearing the packet counter for that entry.

proto:

ip  All packets match.

all All packets match.

tcp Only TCP packets match.

udp Only UDP packets match.

icmp
    Only ICMP packets match.

src and dst:

<address/mask> [ports]

The <address/mask> may be specified as:

ipno
    An ipnumber of the form 1.2.3.4. Only this exact ip number match the rule.

ipno/bits
    An ipnumber with a mask width of the form 1.2.3.4/24. In this case all ip numbers from 1.2.3.0 to 1.2.3.255 will match.

ipno:mask
    An ipnumber with a mask width of the form 1.2.3.4:255.255.240.0 In this case all ip numbers from 1.2.0.0 to 1.2.15.255 will match.

With the TCP and UDP protocols, an optional port may be specified as:

{port|port-port}[,port[,...]]

Service names (from /etc/services) may not be used instead of a numeric port value. Also, note that a range may only be specified as the first value, and the port list is limited to IP_FW_MAX_PORTS (as defined in /usr/src/sys/netinet/ip_fw.h) ports.

If ``via'' name is specified, only packets received via or on their way out of an interface matching name will match this rule.

If ``via'' ipno is specified, only packets received via or on their way out of an interface having the address ipno will match this rule.

options:

frag
    Matches if the packet is a fragment and this is not the first fragment of the datagram.

in  Matches if this packet was on the way in.

out Matches if this packet was on the way out.

ipoptions spec
    Matches if the IP header contains the comma separated list of options specified in spec. The supported IP options are: ssrr (strict source route), lsrr (loose source route), rr (record packet route), and ts (timestamp). The absence of a particular option may be denoted with a ``!''.

established
    Matches packets that have the RST or ACK bits set. TCP packets only.

setup
    Matches packets that have the SYN bit set but no ACK bit. TCP packets only.

tcpflags spec
    Matches if the TCP header contains the comma separated list of flags specified in spec. The supported TCP flags are: fin, syn, rst, psh, ack, and urg. The absence of a particular flag may be denoted with a ``!''.

icmptypes types
    Matches if the ICMP type is in the list types. The list may be specified as any combination of ranges or individual types separated by commas.

CHECKLIST
Here are some important points to consider when designing your rules:

•   Remember that you filter both packets going in and out. Most connections need packets going in both directions.

•   Remember to test very carefully. It is a good idea to be near the console when doing this.

•   Don't forget the loopback interface.

•   Don't filter all if you are also specifying a port.

FINE POINTS
There is one kind of packet that the firewall will always discard, that is an IP fragment with a fragment offset of one. This is a valid packet, but it only has one use, to try to circumvent firewalls.

If you are logged in over a network, loading the LKM version of ipfw is probably not as straightforward as you would think. I recommend this command line:

    modload /lkm/ipfw_mod.o && \
    ipfw add 32000 allow all from any to any

Along the same lines, doing an

    ipfw flush

in similar surroundings is also a bad idea.

EXAMPLES
This command adds an entry which denies all tcp packets from hacker.evil.org to the telnet port of wolf.tambov.su from being forwarded by the host:

    ipfw add deny tcp from hacker.evil.org to wolf.tambov.su 23

This one disallows any connection from the entire hackers network to my host:

    ipfw addf deny all from 123.45.67.0/24 to my.host.org

Here is good usage of list command to see accounting records:

    ipfw -at l

or in short form

    ipfw -a l

SEE ALSO
gethostbyname(3), getservbyport(3), ip(4), ipfirewall(4), ipaccounting(4), reboot(8), syslogd(8)

BUGS

WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!

This program can put your computer in rather unusable state. When using it for the first time, work on the console of the computer, and do NOT do anything you don't understand.

When manipulating/adding chain entries, service names are not accepted.

HISTORY
Initially this utility was written for BSDI by:

    Daniel Boulet <danny@BouletFermat.ab.ca>

The FreeBSD version is written completely by:

    Ugen J.S.Antsilevich <ugen@FreeBSD.ORG>

This has all been extensively rearranged by Poul-Henning Kamp and Alex Nash.