IPFW(8)

NAME
ipfw - controlling utility for IP firewall

SYNOPSIS
ipfw file
ipfw flush
ipfw zero
ipfw delete number
ipfw [ -aN] 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 is 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.

-N  Try to resolve addresses.

action:

accept
    Accept packets that match rule. The search terminates.

pass
    same as accept.

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.

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:

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.

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 is 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
    Not yet documented. Look in the source: src/sys/netnet/ipfw.c.

established
    Matches packets that do not have the SYN bit set. TCP packets only.

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

tcpflags spec
    Not yet documented. Look in the source: src/sys/netnet/ipfw.c. TCP packets only.

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

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

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

•   Don't forget the loopback interface.

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 a

    ipfw flush

in similar surroundings is also a bad idea.

WARNING
This manual page is out of date beyond this point! It is left here until some new text can be written.

OLD
In the first synopsis form, ipfw controls the firewall and accounting chains. In the second synopsis form, ipfw sets the global firewall / accounting properties and show the chain list's contents.

These are the valid entry_actions:

addf[irewall]
    add entry to firewall chain.

delf[irewall]
    remove entry from firewall chain.

adda[ccounting]
    add entry to accounting chain.

dela[ccounting]
    remove entry from accounting chain.

clr[accounting]
    clear counters for accounting chain entry.

If no entry_action is specified, it will default to addf[irewall] or adda[ccounting], depending on the chain_entry_pattern specified.

The valid chain_actions are:

f[lush]
    remove all entries in firewall / accounting chains.

l[ist]
    display all entries in firewall / accounting chains.

z[ero]
    clear chain counters (accounting only).

p[olicy]
    set default policy properties.

The chain_entry_pattern structure is:

    [keyword] [protocol] [address pattern]

For the firewall chain, valid keywords are:

reject
    Reject the packet, and send an ICMP HOST_UNREACHABLE packet to the source.

lreject
    The same as reject, but also log the packets details.

deny
    Reject the packet.

ldeny
    The same as deny, but also log the packets details.

log Accept the packet, and log it.

accept
    Accept the packet (obviously).

pass
    A synonym for accept.

For the accounting chain, valid keywords are:

single
    Log packets matching entry.

bidirectional
    Log packets matching entry and also those going in the opposite direction (from “dst” to “src”).

Each keyword will be recognized by the shortest unambiguous prefix.

Recognized protocols are:

all Matches any IP packet.

icmp
    Matches ICMP packets.

tcp Matches TCP packets.

udp Matches UDP packets.

syn Matches the TCP SYN packet used in initiating a TCP connection. It does not match the packet returned from a destination machine which has the SYN and ACK bits set.

The address pattern is:

    from <address/mask>[ports] to <address/mask][ports] [via <interface>]

You can only specify ports with protocols which actually have ports (TCP, UDP and SYN).

The order of ‘from/to/via’ keywords is unimportant. You can skip any of them, which will be then substituted by default entry matching any ‘from/to/via’ packet kind.

The <address/mask> is defined as:

    <address|name>[/mask_bits|:mask_pattern]

mask bits is the decimal number of bits set in the address mask. mask pattern has the form of an IP address to be AND'ed logically with the address given. The keyword any can be used to specify “any IP”. The IP address or name given is NOT checked, and the wrong value causes the entry to not match anything.

The ports to be blocked are specified as:
    port[,port[,...]]
or:
    port:port

to specify a range of ports. The name of a service (from /etc/services) can be used instead of a numeric port value.

The via <interface> entry is optional and may specify IP address/domain name of local IP interface, or interface name (e.g. ed0) to match only packets coming through this interface. The keyword via can be substituted by on, for readability reasons.

The l[ist] command may be passed:

    f[irewall] | a[ccounting]

to list specific chain or none to list all of chains. The long output format (default) is compatible with the syntax used by the ipfw utility.

The f[lush] command may be passed:

    f[irewall] | a[ccounting]

to remove all entries from firewall or from accounting chain. Without an argument it will remove all entries from both chains.

The z[ero] command needs no arguments. This command clears all counters for the entire accounting chain.

The p[olicy] command can be given

    a[ccept] | d[eny]

to set default policy as denial/acceptance. Without an argument, the current policy status is displayed.

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 addf deny tcp from hacker.evil.org to wolf.tambov.su telnet

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 -sa list accounting

or in short form

    ipfw -sa l a

Many more examples can be found in the file:
    /usr/share/FAQ/ipfw.FAQ
(missing for the moment)

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

BUGS
Currently there is no method for filtering out specific types of ICMP packets. Either you don't filter ICMP at all, or all ICMP packets are filtered.

The system has an optional rule weighting system for the firewall chain. This means that rules are not used in the order that they are specified. To enable this feature, you need to recompile your kernel, see the configuration for details. In general you do not want that.

To see what rule ordering is used, use the list command.

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.

Remember that “ipfw flush” can solve all the problems. Bear in mind that “ipfw policy deny” combined with some wrong chain entry (possible the only entry, which is designed to deny some external packets), can close your computer from the outer world for good (or at least until you can get to the console).

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>

while the synopsis is partially compatible with the old one.

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