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