IPFW(8)

NAME
ipfw - controlling utility for IP firewall

SYNOPSIS
ipfw [-q] [ -p preproc [-D macro[=value]] [-U macro]] file
ipfw [ -f | -q] flush
ipfw [ -q] zero [number ...]
ipfw delete number ...
ipfw [-aftN] list [number ...]
ipfw [ -ftN] show [number ...]
ipfw [ -q] 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.

Optionally, a preprocessor can be specified using -p preproc where file is to be piped through. Useful preprocessors include cpp(1) and m4(1). If preproc doesn't start with a slash as its first character, the usual PATH name search is performed. Care should be taken with this in environments where not all filesystems are mounted (yet) by the time ipfw is being run (e. g. since they are mounted over NFS). Once -p has been specified, optional -D and -U specifications can follow and will be passed on to the preprocessor. This allows for flexible configuration files (like conditionalizing them on the local hostname) and the use of macros to centralize frequently required arguments like IP addresses.

Each packet that has been received or is about to be sent goes through the ipfw rules. In the case of a host acting as a gateway, packets that are forwarded by the host are processed by ipfw twice ( once when entering, and once when leaving). Each packet can be filtered based on the following information that is associated with it:

Receive Interface (recv)
    Interface over which the packet was received
Transmit Interface (xmit)
    Interface over which the packet would be transmitted
Incoming (in)
    Packet was just received
Outgoing (out)
    Packet would be transmitted
Source IP Address
    Sender's IP Address
Destination IP Address
    Target's IP Address
Protocol
    IP protocol, including but not limited to IP (ip), UDP (udp), TCP (tcp), or ICMP (icmp)
Source Port
    Sender's UDP or TCP port
Destination Port
    Target's UDP or TCP port
Connection Setup Flag (setup)
    This packet is a request to setup a TCP connection
Connection Established Flag (established)
    This packet is part of an established TCP connection
All TCP Flags (tcpflags)
    One or more of the TCP flags: close connection (fin), open connection (syn), reset connection (rst), push (psh), acknowledgment (ack), and urgent (urg)
Fragment Flag (frag)
    This packet is a fragment of an IP packet
IP Options (ipoptions)
    One or more of the IP options: strict source route (ssrr), loose source route (lsrr), record route (rr), and timestamp (ts)
ICMP Types (icmptypes)
    One or more of the ICMP types: echo reply (0), destination unreachable (3), source quench (4), redirect (5), echo request (8), router advertisement (9), router solicitation (10), time-to-live exceeded (11), IP header bad (12), timestamp request (13), timestamp reply (14), information request (15), information reply (16), address mask request (17), and address mask reply (18)

Note that may be dangerous to filter on the source IP address or source TCP/UDP port because either or both could easily be spoofed.

The ipfw code works by going through the rule-list for each packet until a match is found. All rules have two associated counters, 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” from 1 to 65534 that is used to order and delete rules. Rules are tried in increasing order, and the first rule that matches a packet applies. Multiple rules may share the same number and apply in the order in which they were added.

If a rule is added without a number, it is numbered 100 higher than the highest defined rule number, unless the highest defined rule number is 65435 or greater, in which case new rules are given that same number.

The delete operation deletes the first rule with number number, if any.

The list command prints out the current rule set.

The show command is equivalent to ‘ipfw -a list’.

The zero operation zeroes the counters associated with rule number number.

The flush operation removes all rules.

Any command beginning with a ‘#’, or being all blank, is ignored.

One rule is always present:

    65535 deny all from any to any

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

However, if the kernel option “IPFIREWALL_DEFAULT_TO_ACCEPT” is active, the rule is instead:

    65535 allow all from any to any

This variation lets everything pass through. This option should only be activated in particular circumstances, such as if you use the firewall system as an on-demand denial-of-service filter that is normally wide open.

The following options are available:

-a  While listing, show counter values. See also “show” command.

-f  Don't ask for confirmation for commands that can cause problems if misused (i.e. flush). Note, if there is no tty associated with the process, this is implied.

-q  While adding, zeroing or flushing, be quiet about actions (implies '-f'). This is useful for adjusting rules by executing multiple ipfw commands in a script ( e.g., ‘sh /etc/rc.firewall’), or by processing a file of many ipfw rules, across a remote login session. If a flush is performed in normal (verbose) mode (with the default kernel configuration), it prints a message. Because all rules are flushed, the message cannot be delivered to the login session. This causes the remote login session to be closed and the remainder of the ruleset is not processed. Access to the console is required to recover.

-t  While listing, show last match timestamp.

-N  Try to resolve addresses and service names in output.

action:

allow
    Allow packets that match rule. The search terminates. Aliases are pass, permit, and accept.

deny
    Discard packets that match this rule. The search terminates. Drop is an alias for deny.

reject
    (Deprecated.) Discard packets that match this rule, and try to send an ICMP host unreachable notice. The search terminates.

unreach code
    Discard packets that match this rule, and try to send an ICMP unreachable notice with code code, where code is a number from zero to 255, or one of these aliases: net, host, protocol, port, needfrag, srcfail, net-unknown, host-unknown, isolated, net-prohib, host-prohib, tosnet, toshost, filter-prohib, host-precedence, or precedence-cutoff. The search terminates.

reset
    TCP packets only. Discard packets that match this rule, and try to send a TCP reset (RST) notice. The search terminates.

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

divert port
    Divert packets that match this rule to the divert(4) socket bound to port port. The search terminates.

tee port
    Send a copy of packets matching this rule to the divert(4) socket bound to port port. The search continues with the next rule. This feature is not yet implemeted.

fwd ipaddr [,port]
    Change the next-hop on matching packets to ipaddr, which can be an IP address in dotted quad or a host name. If ipaddr is not a directly-reachable address, the route as found in the local routing table for that IP is used instead. If ipaddr is a local address, then on a packet entering the system from a remote host it will be diverted to port on the local machine, keeping the local address of the socket set to the original IP address the packet was destined for. This is intended for use with transparent proxy servers. If the IP is not a local address then the port number (if specified) is ignored and the rule only applies to packets leaving the system. This will also map addresses to local ports when packets are generated locally. The search terminates if this rule matches. If the port number is not given then the port number in the packet is used, so that a packet for an external machine port Y would be forwarded to local port Y. The kernel must have been compiled with optiions IPFIREWALL_FORWARD.

skipto number
    Skip all subsequent rules numbered less than number. The search continues with the first rule numbered number or higher.

If a packet matches more than one divert and/or tee rule, all but the last are ignored.

If the kernel was compiled with IPFIREWALL_VERBOSE, then 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 IPFIREWALL_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.

Console logging and the log limit are adjustable dynamically through the sysctl(8) interface.

proto:

ip  All packets match. The alias all has the same effect.

tcp Only TCP packets match.

udp Only UDP packets match.

icmp
    Only ICMP packets match.

<number|name>
    Only packets for the specified protocol matches (see /etc/protocols for a complete list).

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.

The sense of the match can be inverted by preceding an address with the “not” modifier, causing all other addresses to be matched instead. This does not affect the selection of port numbers.

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

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

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

Fragmented packets which have a non-zero offset (i.e. not the first fragment) will never match a rule which has one or more port specifications. See the frag option for details on matching fragmented packets.

Rules can apply to packets when they are incoming, or outgoing, or both. The in keyword indicates the rule should only match incoming packets. The out keyword indicates the rule should only match outgoing packets.

To match packets going through a certain interface, specify the interface using via:

via ifX
    Packet must be going through interface ifX.

via if*
    Packet must be going through interface ifX, where X is any unit number.

via any
    Packet must be going through some interface.

via ipno
    Packet must be going through the interface having IP address ipno.

The via keyword causes the interface to always be checked. If recv or xmit is used instead of via, then the only receive or transmit interface (respectively) is checked. By specifying both, it is possible to match packets based on both receive and transmit interface, e.g.:

    ipfw add 100 deny ip from any to any out recv ed0 xmit ed1

The recv interface can be tested on either incoming or outgoing packets, while the xmit interface can only be tested on outgoing packets. So out is required (and in invalid) whenver xmit is used. Specifying via together with xmit or recv is invalid.

A packet may not have a receive or transmit interface: packets originating from the local host have no receive interface. while packets destined for the local host have no transmit interface.

Additional options:

frag
    Matches if the packet is a fragment and this is not the first fragment of the datagram. frag may not be used in conjunction with either tcpflags or TCP/UDP port specifications.

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 “”!. A rule which contains a tcpflags specification can never match a fragmented packet which has a non-zero offset. See the frag option for details on matching fragmented packets.

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.

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 KLD version of ipfw is probably not as straightforward as you would think. I recommend this command line:

    kldload /modules/ipfw.ko && \
    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.

The IP filter list may not be modified if the system security level is set to 3 or higher ( see init(8) for information on system security levels).

PACKET DIVERSION
A divert socket bound to the specified port will receive all packets diverted to that port; see divert(4). If no socket is bound to the destination port, or if the kernel wasn't compiled with divert socket support, diverted packets are dropped.

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

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

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

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

Here is a good usage of the list command to see accounting records and timestamp information:

    ipfw -at l

or in short form without timestamps:

    ipfw -a l

This rule diverts all incoming packets from 192.168.2.0/24 to divert port 5000:

    ipfw divert 5000 all from 192.168.2.0/24 to any in

SEE ALSO
cpp(1), m4(1), divert(4), ip(4), ipfirewall(4), protocols(5), services(5), init(8), kldload(8), reboot(8), sysctl(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 and protocol names are not accepted.

Incoming packet fragments diverted by divert are reassembled before delivery to the socket, whereas fragments diverted via tee are not.

Port aliases containing dashes cannot be first in a list.

The “tee” action is unimplemented.

AUTHORS
Ugen J. S. Antsilevich, Poul-Henning Kamp, Alex Nash, Archie Cobbs. API based upon code written by Daniel Boulet for BSDI.

HISTORY
ipfw first appeared in FreeBSD 2.0.