ipfw.8 revision 38092
1.Dd July 20, 1996
2.Dt IPFW 8 SMM
3.Os FreeBSD
4.Sh NAME
5.Nm ipfw
6.Nd controlling utility for IP firewall
7.Sh SYNOPSIS
8.Nm ipfw
9.Oo
10.Fl q
11.Oc
12file
13.Nm ipfw
14.Oo
15.Fl f
16|
17.Fl q
18.Oc
19flush
20.Nm ipfw
21.Oo
22.Fl q
23.Oc
24zero
25.Op Ar number ...
26.Nm ipfw
27delete
28.Ar number ...
29.Nm ipfw
30.Op Fl aftN
31list
32.Op Ar number ...
33.Nm ipfw
34.Oo
35.Fl ftN
36.Oc
37show
38.Op Ar number ...
39.Nm ipfw
40.Oo
41.Fl q
42.Oc
43add
44.Op Ar number
45.Ar action 
46.Op log
47.Ar proto
48from
49.Ar src
50to
51.Ar dst
52.Op via Ar name | ipno
53.Op Ar options
54.Sh DESCRIPTION
55If used as shown in the first synopsis line, the
56.Ar file
57will be read line by line and applied as arguments to the 
58.Nm
59command.
60.Pp
61The
62.Nm
63code works by going through the rule-list for each packet,
64until a match is found.
65All rules have two associated counters, a packet count and
66a byte count.
67These counters are updated when a packet matches the rule.
68.Pp
69The rules are ordered by a ``line-number'' from 1 to 65534 that is used
70to order and delete rules. Rules are tried in increasing order, and the
71first rule that matches a packet applies.
72Multiple rules may share the same number and apply in
73the order in which they were added.
74.Pp
75If a rule is added without a number, it is numbered 100 higher
76than the previous rule. If the highest defined rule number is
77greater than 65534, new rules are appended to the last rule.
78.Pp
79The delete operation deletes the first rule with number
80.Ar number ,
81if any.
82.Pp
83The list command prints out the current rule set.
84.Pp
85The show command is equivalent to `ipfw -a list'.
86.Pp
87The zero operation zeroes the counters associated with rule number
88.Ar number .
89.Pp
90The flush operation removes all rules.
91.Pp
92Any command beginning with a '#', or being all blank, is ignored.
93.Pp
94One rule is always present:
95.Bd -literal -offset center
9665535 deny all from any to any
97.Ed
98.Pp
99This rule is the default policy, i.e., don't allow anything at all.
100Your job in setting up rules is to modify this policy to match your
101needs.
102.Pp
103However, if the kernel option
104.Dq IPFIREWALL_DEFAULT_TO_ACCEPT
105is active, the rule is instead:
106.Bd -literal -offset center
10765535 allow all from any to any
108.Ed
109.Pp
110This variation lets everything pass through.  This option should only be
111activated in particular circumstances, such as if you use the firewall
112system as an on-demand denial-of-service filter that is normally wide open.
113.Pp
114The following options are available:
115.Bl -tag -width flag
116.It Fl a
117While listing, show counter values.  See also ``show'' command.
118.It Fl f
119Don't ask for confirmation for commands that can cause problems if misused
120(ie; flush).
121.Ar Note ,
122if there is no tty associated with the process, this is implied.
123.It Fl q
124While adding, zeroing or flushing, be quiet about actions (implies '-f'). 
125This is useful for adjusting rules by executing multiple ipfw commands in a
126script (e.g. sh /etc/rc.firewall), or by processing a file of many ipfw rules,
127across a remote login session.  If a flush is performed in normal
128(verbose) mode (with the default kernel configuration), it prints a message.
129Because all rules are flushed, the
130message cannot be delivered to the login session, the login session is
131closed and the remainder of the ruleset is not processed.  Access to the
132console is required to recover.
133.It Fl t
134While listing, show last match timestamp.
135.It Fl N
136Try to resolve addresses and service names in output.
137.El
138.Pp
139.Ar action :
140.Bl -hang -offset flag -width 1234567890123456
141.It Ar allow
142Allow packets that match rule.
143The search terminates. Aliases are
144.Ar pass ,
145.Ar permit ,
146and
147.Ar accept .
148.It Ar deny
149Discard packets that match this rule.
150The search terminates.
151.Ar Drop
152is an alias for
153.Ar deny .
154.It Ar reject
155(Deprecated.) Discard packets that match this rule, and try to send an ICMP
156host unreachable notice.
157The search terminates.
158.It Ar unreach code
159Discard packets that match this rule, and try to send an ICMP
160unreachable notice with code
161.Ar code ,
162where
163.Ar code
164is a number from zero to 255, or one of these aliases:
165.Ar net ,
166.Ar host ,
167.Ar protocol ,
168.Ar port ,
169.Ar needfrag ,
170.Ar srcfail ,
171.Ar net-unknown ,
172.Ar host-unknown ,
173.Ar isolated ,
174.Ar net-prohib ,
175.Ar host-prohib ,
176.Ar tosnet ,
177.Ar toshost ,
178.Ar filter-prohib ,
179.Ar host-precedence ,
180or
181.Ar precedence-cutoff .
182The search terminates.
183.It Ar reset
184TCP packets only. Discard packets that match this rule,
185and try to send a TCP reset (RST) notice.
186The search terminates.
187.It Ar count
188Update counters for all packets that match rule.
189The search continues with the next rule.
190.It Ar divert port
191Divert packets that match this rule to the
192.Xr divert 4
193socket bound to port
194.Ar port .
195The search terminates.
196.It Ar tee port
197Send a copy of packets matching this rule to the
198.Xr divert 4
199socket bound to port
200.Ar port .
201The search continues with the next rule. This feature is not yet implemeted.
202.It Ar fwd ipaddr Op ,port
203Change the next-hop on matching packets to
204.Ar ipaddr ,
205which can be an IP address in dotted quad or a host name.
206If
207.Ar ipaddr
208is not a directly-reachable address, the route 
209as found in the local routing table for that IP is used
210instead.
211If
212.Ar ipaddr
213is a local address, then on a packet entering the system from a remote
214host it will be diverted to
215.Ar port
216on the local machine, keeping the local address of the socket set
217to the original IP address the packet was destined for. This is intended
218for use with transparent proxy servers. If the IP is not
219a local address then the port number (if specified) is ignored and
220the rule only applies to packets leaving the system. This will
221also map addresses to local ports when packets are generated locally.
222The search terminates if this rule matches. If the port number is not 
223given then the port number in the packet is used, so that a packet for
224an external machine port Y would be forwarded to local port Y. The kernel
225must have been compiled with optiions IPFIREWALL_FORWARD.
226.It Ar skipto number
227Skip all subsequent rules numbered less than
228.Ar number .
229The search continues with the first rule numbered
230.Ar number
231or higher.
232.El
233.Pp
234If a packet matches more than one
235.Ar divert
236and/or
237.Ar tee
238rule, all but the last are ignored.
239.Pp
240If the kernel was compiled with
241.Dv IPFIREWALL_VERBOSE ,
242then when a packet matches a rule with the ``log''
243keyword a message will be printed on the console.
244If the kernel was compiled with the
245.Dv IPFIREWALL_VERBOSE_LIMIT
246option, then logging will cease after the number of packets
247specified by the option are received for that particular
248chain entry.  Logging may then be re-enabled by clearing
249the packet counter for that entry.
250.Pp
251Console logging and the log limit are adjustable dynamically
252through the
253.Xr sysctl 8
254interface.
255.Pp
256.Ar proto :
257.Bl -hang -offset flag -width 1234567890123456
258.It Ar ip
259All packets match. The alias
260.Ar all
261has the same effect.
262.It Ar tcp
263Only TCP packets match.
264.It Ar udp
265Only UDP packets match.
266.It Ar icmp
267Only ICMP packets match.
268.It Ar <number|name>
269Only packets for the specified protocol matches (see
270.Pa /etc/protocols
271for a complete list).
272.El
273.Pp
274.Ar src 
275and
276.Ar dst :
277.Bl -hang -offset flag
278.It Ar <address/mask>
279.Op Ar ports
280.El
281.Pp
282The
283.Em <address/mask>
284may be specified as:
285.Bl -hang -offset flag -width 1234567890123456
286.It Ar ipno
287An ipnumber of the form 1.2.3.4.
288Only this exact ip number match the rule.
289.It Ar ipno/bits
290An ipnumber with a mask width of the form 1.2.3.4/24.
291In this case all ip numbers from 1.2.3.0 to 1.2.3.255 will match.
292.It Ar ipno:mask
293An ipnumber with a mask width of the form 1.2.3.4:255.255.240.0.
294In this case all ip numbers from 1.2.0.0 to 1.2.15.255 will match.
295.El
296.Pp
297The sense of the match can be inverted by preceding an address with the
298``not'' modifier, causing all other addresses to be matched instead. This
299does not affect the selection of port numbers.
300.Pp
301With the TCP and UDP protocols, optional
302.Em ports
303may be specified as:
304.Pp
305.Bl -hang -offset flag
306.It Ns {port|port-port} Ns Op ,port Ns Op ,...
307.El
308.Pp
309Service names (from 
310.Pa /etc/services )
311may be used instead of numeric port values.
312A range may only be specified as the first value,
313and the length of the port list is limited to
314.Dv IP_FW_MAX_PORTS
315(as defined in 
316.Pa /usr/src/sys/netinet/ip_fw.h )
317ports.
318.Pp
319Fragmented packets which have a non-zero offset (i.e. not the first
320fragment) will never match a rule which has one or more port
321specifications.  See the
322.Ar frag
323option for details on matching fragmented packets.
324.Pp
325Rules can apply to packets when they are incoming, or outgoing, or both.
326The
327.Ar in
328keyword indicates the rule should only match incoming packets.
329The
330.Ar out
331keyword indicates the rule should only match outgoing packets.
332.Pp
333To match packets going through a certain interface, specify
334the interface using
335.Ar via :
336.Bl -hang -offset flag -width 1234567890123456
337.It Ar via ifX
338Packet must be going through interface
339.Ar ifX.
340.It Ar via if*
341Packet must be going through interface
342.Ar ifX ,
343where X is any unit number.
344.It Ar via any
345Packet must be going through
346.Em some
347interface.
348.It Ar via ipno
349Packet must be going through the interface having IP address
350.Ar ipno .
351.El
352.Pp
353The
354.Ar via
355keyword causes the interface to always be checked.
356If
357.Ar recv
358or
359.Ar xmit
360is used instead of
361.Ar via ,
362then the only receive or transmit interface (respectively) is checked.
363By specifying both, it is possible to match packets based on both receive
364and transmit interface, e.g.:
365.Pp
366.Dl "ipfw add 100 deny ip from any to any out recv ed0 xmit ed1"
367.Pp
368The
369.Ar recv
370interface can be tested on either incoming or outgoing packets, while the
371.Ar xmit
372interface can only be tested on outgoing packets. So
373.Ar out
374is required (and
375.Ar in
376invalid) whenver
377.Ar xmit
378is used. Specifying
379.Ar via
380together with
381.Ar xmit
382or
383.Ar recv
384is invalid.
385.Pp
386A packet may not have a receive or transmit interface: packets originating
387from the local host have no receive interface. while packets destined for
388the local host have no transmit interface.
389.Pp
390Additional
391.Ar options :
392.Bl -hang -offset flag -width 1234567890123456
393.It frag
394Matches if the packet is a fragment and this is not the first fragment
395of the datagram.
396.Ar frag
397may not be used in conjunction with either
398.Ar tcpflags
399or TCP/UDP port specifications.
400.It in
401Matches if this packet was on the way in.
402.It out
403Matches if this packet was on the way out.
404.It ipoptions Ar spec
405Matches if the IP header contains the comma separated list of 
406options specified in
407.Ar spec .
408The supported IP options are:
409.Ar ssrr 
410(strict source route),
411.Ar lsrr 
412(loose source route),
413.Ar rr 
414(record packet route), and
415.Ar ts 
416(timestamp).
417The absence of a particular option may be denoted
418with a ``!''.
419.It established
420Matches packets that have the RST or ACK bits set.
421TCP packets only.
422.It setup
423Matches packets that have the SYN bit set but no ACK bit.
424TCP packets only.
425.It tcpflags Ar spec
426Matches if the TCP header contains the comma separated list of
427flags specified in
428.Ar spec .
429The supported TCP flags are:
430.Ar fin ,
431.Ar syn ,
432.Ar rst ,
433.Ar psh ,
434.Ar ack ,
435and
436.Ar urg .
437The absence of a particular flag may be denoted
438with a ``!''.
439A rule which contains a
440.Ar tcpflags
441specification can never match a fragmented packet which has
442a non-zero offset.  See the
443.Ar frag
444option for details on matching fragmented packets.
445.It icmptypes Ar types
446Matches if the ICMP type is in the list
447.Ar types .
448The list may be specified as any combination of ranges
449or individual types separated by commas.
450.El
451.Sh CHECKLIST
452Here are some important points to consider when designing your
453rules:
454.Bl -bullet -hang -offset flag 
455.It 
456Remember that you filter both packets going in and out.
457Most connections need packets going in both directions.
458.It
459Remember to test very carefully.
460It is a good idea to be near the console when doing this.
461.It
462Don't forget the loopback interface.
463.El
464.Sh FINE POINTS
465There is one kind of packet that the firewall will always discard,
466that is an IP fragment with a fragment offset of one.
467This is a valid packet, but it only has one use, to try to circumvent
468firewalls.
469.Pp
470If you are logged in over a network, loading the LKM version of
471.Nm
472is probably not as straightforward as you would think.
473I recommend this command line:
474.Bd -literal -offset center
475modload /lkm/ipfw_mod.o && \e
476ipfw add 32000 allow all from any to any
477.Ed
478.Pp
479Along the same lines, doing an
480.Bd -literal -offset center
481ipfw flush
482.Ed
483.Pp
484in similar surroundings is also a bad idea.
485.Sh PACKET DIVERSION
486A divert socket bound to the specified port will receive all packets diverted
487to that port; see
488.Xr divert 4 .
489If no socket is bound to the destination port, or if the kernel
490wasn't compiled with divert socket support, diverted packets are dropped.
491.Sh EXAMPLES
492This command adds an entry which denies all tcp packets from
493.Em cracker.evil.org
494to the telnet port of
495.Em wolf.tambov.su
496from being forwarded by the host:
497.Pp
498.Dl ipfw add deny tcp from cracker.evil.org to wolf.tambov.su 23
499.Pp 
500This one disallows any connection from the entire crackers network to
501my host:
502.Pp
503.Dl ipfw addf deny all from 123.45.67.0/24 to my.host.org
504.Pp
505Here is a good usage of the list command to see accounting records
506and timestamp information:
507.Pp
508.Dl ipfw -at l
509.Pp
510or in short form without timestamps:
511.Pp
512.Dl ipfw -a l
513.Pp
514This rule diverts all incoming packets from 192.168.2.0/24 to divert port 5000:
515.Pp
516.Dl ipfw divert 5000 all from 192.168.2.0/24 to any in
517.Sh SEE ALSO
518.Xr divert 4 ,
519.Xr ip 4 ,
520.Xr ipfirewall 4 ,
521.Xr protocols 5 ,
522.Xr services 5 ,
523.Xr reboot 8 ,
524.Xr sysctl 8 ,
525.Xr syslogd 8
526.Sh BUGS
527.Pp
528.Em WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!
529.Pp
530This program can put your computer in rather unusable state. When
531using it for the first time, work on the console of the computer, and
532do
533.Em NOT
534do anything you don't understand.
535.Pp
536When manipulating/adding chain entries, service and protocol names are
537not accepted.
538.Pp
539Incoming packet fragments diverted by
540.Ar divert
541are reassembled before delivery to the socket, whereas fragments diverted via
542.Ar tee
543are not.
544.Pp
545Port aliases containing dashes cannot be first in a list.
546.Sh AUTHORS
547.An Ugen J. S. Antsilevich ,
548.An Poul-Henning Kamp ,
549.An Alex Nash ,
550.An Archie Cobbs .
551API based upon code written by
552.An Daniel Boulet
553for BSDI.
554.Sh HISTORY
555.Nm
556first appeared in
557.Fx 2.0 .
558