ipfw.8 revision 20837
1110211Smarcel.Dd July 20, 1996
2110211Smarcel.Dt IPFW 8 SMM
3110211Smarcel.Os FreeBSD
4110211Smarcel.Sh NAME
5110211Smarcel.Nm ipfw
6110211Smarcel.Nd controlling utility for IP firewall
7110211Smarcel.Sh SYNOPSIS
8110211Smarcel.Nm
9110211Smarcel.Ar file
10110211Smarcel.Nm ipfw
11110211Smarcelflush
12110211Smarcel.Nm ipfw
13110211Smarcelzero
14110211Smarcel.Op Ar number
15110211Smarcel.Nm ipfw
16110211Smarceldelete
17110211Smarcel.Ar number
18110211Smarcel.Nm ipfw
19110211Smarcel.Op Fl aftN
20110211Smarcellist
21110211Smarcel.Nm ipfw
22110211Smarceladd
23110211Smarcel.Op Ar number
24110211Smarcel.Ar action 
25110211Smarcel.Op Ar log
26110211Smarcel.Ar proto
27110211Smarcelfrom
28110211Smarcel.Ar src
29110211Smarcelto
30110211Smarcel.Ar dst
31110211Smarcel.Oo
32110211Smarcelvia
33110211Smarcel.Ar name | ipno
34110211Smarcel.Oc
35110211Smarcel.Op Ar options
36110211Smarcel.Sh DESCRIPTION
37110211SmarcelIf used as shown in the first synopsis line, the
38110211Smarcel.Ar file
39110211Smarcelwill be read line by line and applied as arguments to the 
40110211Smarcel.Nm
41110211Smarcelcommand.
42110211Smarcel.Pp
43110211SmarcelThe
44110211Smarcel.Nm
45110211Smarcelcode works by going through the rule-list for each packet,
46110211Smarceluntil a match is found.
47110211SmarcelAll rules have two associated counters, a packet count and
48110211Smarcela byte count.
49110211SmarcelThese counters are updated when a packet matches the rule.
50110211Smarcel.Pp
51110211SmarcelThe rules are ordered by a ``line-number'' from 1 to 65534 that is used
52110211Smarcelto order and delete rules. Rules are tried in increasing order, and the
53110211Smarcelfirst rule that matches a packet applies.
54110211SmarcelMultiple rules may share the same number and apply in
55110211Smarcelthe order in which they were added.
56110211Smarcel.Pp
57110211SmarcelIf a rule is added without a number, it numbered 100 higher
58110211Smarcelthan the previous rule. If the highest defined rule number is
59110211Smarcelgreater than 65434, new rules are appended to the last rule.
60110211Smarcel.Pp
61110211SmarcelThe delete operation deletes the first rule with number
62110211Smarcel.Ar number ,
63110211Smarcelif any.
64110211Smarcel.Pp
65110211SmarcelThe list command prints out the current rule set.
66110211Smarcel.Pp
67110211SmarcelThe zero operation zeroes the counters associated with rule number
68110211Smarcel.Ar number .
69110211Smarcel.Pp
70110211SmarcelThe flush operation removes all rules.
71110211Smarcel.Pp
72110211SmarcelOne rule is always present:
73110211Smarcel.Bd -literal -offset center
74110211Smarcel65535 deny all from any to any
75110211Smarcel.Ed
76110211Smarcel.Pp
77110211SmarcelThis rule is the default policy, i.e., don't allow anything at all.
78110211SmarcelYour job in setting up rules is to modify this policy to match your needs.
79110211Smarcel.Pp
80110211SmarcelThe following options are available:
81110211Smarcel.Bl -tag -width flag
82110211Smarcel.It Fl a
83110211SmarcelWhile listing, show counter values. This option is the only way to see
84110211Smarcelaccounting records.
85110211Smarcel.It Fl f
86110211SmarcelDon't ask for confirmation for commands that can cause problems if misused
87110211Smarcel(ie; flush).
88110211Smarcel.Ar Note ,
89110211Smarcelif there is no tty associated with the process, this is implied.
90110211Smarcel.It Fl t
91110211SmarcelWhile listing, show last match timestamp.
92110211Smarcel.It Fl N
93110211SmarcelTry to resolve addresses and service names in output.
94110211Smarcel.El
95110211Smarcel.Pp
96110211Smarcel.Ar action :
97110211Smarcel.Bl -hang -offset flag -width 1234567890123456
98110211Smarcel.It Ar allow
99110211SmarcelAllow packets that match rule.
100110211SmarcelThe search terminates.
101110211Smarcel.It Ar pass
102110211SmarcelSame as allow.
103110211Smarcel.It Ar accept
104110211SmarcelSame as allow.
105110211Smarcel.It Ar count
106110211SmarcelUpdate counters for all packets that match rule.
107110211SmarcelThe search continues with the next rule.
108110211Smarcel.It Ar deny
109110211SmarcelDiscard packets that match this rule.
110110211SmarcelThe search terminates.
111110211Smarcel.It Ar reject
112110211SmarcelDiscard packets that match this rule, and try to send an ICMP notice.
113110211SmarcelThe search terminates.
114110211Smarcel.It Ar divert port
115110211SmarcelDivert packets that match this rule to the divert socket bound to port
116110211Smarcel.Ar port .
117110211SmarcelThe search terminates.
118110211Smarcel.El
119110211Smarcel.Pp
120110211SmarcelWhen a packet matches a rule with the
121110211Smarcel.Ar log
122110211Smarcelkeyword, a message will be printed on the console.
123110211SmarcelIf the kernel was compiled with the
124110211Smarcel.Dv IP_FIREWALL_VERBOSE_LIMIT
125110211Smarceloption, then logging will cease after the number of packets
126110211Smarcelspecified by the option are received for that particular
127110211Smarcelchain entry.  Logging may then be re-enabled by clearing
128110211Smarcelthe packet counter for that entry.
129110211Smarcel.Pp
130110211Smarcel.Ar proto :
131110211Smarcel.Bl -hang -offset flag -width 1234567890123456
132110211Smarcel.It Ar ip
133110211SmarcelAll packets match.
134110211Smarcel.It Ar all
135110211SmarcelAll packets match.
136110211Smarcel.It Ar tcp
137110211SmarcelOnly TCP packets match.
138110211Smarcel.It Ar udp
139110211SmarcelOnly UDP packets match.
140110211Smarcel.It Ar icmp
141110211SmarcelOnly ICMP packets match.
142110211Smarcel.It Ar <number|name>
143110211SmarcelOnly packets for the specified protocol matches (see
144110211Smarcel.Pa /etc/protocols
145110211Smarcelfor a complete list).
146110211Smarcel.El
147110211Smarcel.Pp
148110211Smarcel.Ar src 
149110211Smarceland
150110211Smarcel.Ar dst :
151110211Smarcel.Pp
152110211Smarcel.Bl -hang -offset flag
153110211Smarcel.It <address/mask> [ports]
154110211Smarcel.El
155110211Smarcel.Pp
156110211SmarcelThe
157110211Smarcel.Em <address/mask>
158110211Smarcelmay be specified as:
159110211Smarcel.Bl -hang -offset flag -width 1234567890123456
160110211Smarcel.It Ar ipno
161110211SmarcelAn ipnumber of the form 1.2.3.4.
162110211SmarcelOnly this exact ip number match the rule.
163110211Smarcel.It Ar ipno/bits
164110211SmarcelAn ipnumber with a mask width of the form 1.2.3.4/24.
165110211SmarcelIn this case all ip numbers from 1.2.3.0 to 1.2.3.255 will match.
166110211Smarcel.It Ar ipno:mask
167110211SmarcelAn ipnumber with a mask width of the form 1.2.3.4:255.255.240.0.
168110211SmarcelIn this case all ip numbers from 1.2.0.0 to 1.2.15.255 will match.
169110211Smarcel.El
170110211Smarcel.Pp
171110211SmarcelWith the TCP and UDP
172110211Smarcel.Em protocols ,
173110211Smarcelan optional
174110211Smarcel.Em port
175110211Smarcelmay be specified as:
176110211Smarcel.Pp
177110211Smarcel.Bl -hang -offset flag
178110211Smarcel.It Ns {port|port-port} Ns Op ,port Ns Op ,...
179110211Smarcel.El
180110211Smarcel.Pp
181110211SmarcelService names (from 
182110211Smarcel.Pa /etc/services )
183110211Smarcelmay not be used instead of a numeric port value.
184110211SmarcelAlso, note that a range may only be specified as the first value,
185110211Smarceland the port list is limited to
186110211Smarcel.Dv IP_FW_MAX_PORTS
187110211Smarcel(as defined in 
188110211Smarcel.Pa /usr/src/sys/netinet/ip_fw.h )
189110211Smarcelports.
190110211Smarcel.Pp
191110211SmarcelIf ``via''
192110211Smarcel.Ar name
193110211Smarcelis specified, only packets received via or on their way out of an interface
194matching
195.Ar name
196will match this rule.
197.Pp
198If ``via''
199.Ar ipno
200is specified, only packets received via or on their way out of an interface
201having the address
202.Ar ipno
203will match this rule.
204.Pp
205.Ar options :
206.Bl -hang -offset flag -width 1234567890123456
207.It frag
208Matches if the packet is a fragment and this is not the first fragment
209of the datagram.
210.It in
211Matches if this packet was on the way in.
212.It out
213Matches if this packet was on the way out.
214.It ipoptions Ar spec
215Matches if the IP header contains the comma separated list of 
216options specified in
217.Ar spec .
218The supported IP options are:
219.Ar ssrr 
220(strict source route),
221.Ar lsrr 
222(loose source route),
223.Ar rr 
224(record packet route), and
225.Ar ts 
226(timestamp).
227The absence of a particular option may be denoted
228with a ``!''.
229.It established
230Matches packets that have the RST or ACK bits set.
231TCP packets only.
232.It setup
233Matches packets that have the SYN bit set but no ACK bit.
234TCP packets only.
235.It tcpflags Ar spec
236Matches if the TCP header contains the comma separated list of
237flags specified in
238.Ar spec .
239The supported TCP flags are:
240.Ar fin ,
241.Ar syn ,
242.Ar rst ,
243.Ar psh ,
244.Ar ack ,
245and
246.Ar urg .
247The absence of a particular flag may be denoted
248with a ``!''.
249.It icmptypes Ar types
250Matches if the ICMP type is in the list
251.Ar types .
252The list may be specified as any combination of ranges
253or individual types separated by commas.
254.El
255.Sh CHECKLIST
256Here are some important points to consider when designing your
257rules:
258.Bl -bullet -hang -offset flag 
259.It 
260Remember that you filter both packets going in and out.
261Most connections need packets going in both directions.
262.It
263Remember to test very carefully.
264It is a good idea to be near the console when doing this.
265.It
266Don't forget the loopback interface.
267.El
268.Sh FINE POINTS
269There is one kind of packet that the firewall will always discard,
270that is an IP fragment with a fragment offset of one.
271This is a valid packet, but it only has one use, to try to circumvent
272firewalls.
273.Pp
274If you are logged in over a network, loading the LKM version of
275.Nm
276is probably not as straightforward as you would think.
277I recommend this command line:
278.Bd -literal -offset center
279modload /lkm/ipfw_mod.o && \e
280ipfw add 32000 allow all from any to any
281.Ed
282.Pp
283Along the same lines, doing an
284.Bd -literal -offset center
285ipfw flush
286.Ed
287.Pp
288in similar surroundings is also a bad idea.
289.Sh PACKET DIVERSION
290A divert socket bound to the specified port will receive all packets diverted
291to that port; see
292.Xr divert 4 .
293If no socket is bound to the destination port, or if the kernel
294wasn't compiled with divert socket support, diverted packets are dropped.
295.Sh EXAMPLES
296This command adds an entry which denies all tcp packets from
297.Em hacker.evil.org
298to the telnet port of
299.Em wolf.tambov.su
300from being forwarded by the host:
301.Pp
302.Dl ipfw add deny tcp from hacker.evil.org to wolf.tambov.su 23
303.Pp 
304This one disallows any connection from the entire hackers network to
305my host:
306.Pp
307.Dl ipfw addf deny all from 123.45.67.0/24 to my.host.org
308.Pp
309Here is good usage of list command to see accounting records:
310.Pp
311.Dl ipfw -at l
312.Pp
313or in short form
314.Pp
315.Dl ipfw -a l
316.Pp
317This rule diverts all incoming packets from 192.168.2.0/24 to divert port 5000:
318.Pp
319.Dl ipfw divert 5000 all from 192.168.2.0/24 to any in
320.Sh SEE ALSO
321.Xr divert 4 ,
322.Xr ip 4 ,
323.Xr ipfirewall 4 ,
324.Xr protocols 5 ,
325.Xr services 5 ,
326.Xr reboot 8 ,
327.Xr syslogd 8
328.Sh BUGS
329.Pp
330.Em WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!
331.Pp
332This program can put your computer in rather unusable state. When
333using it for the first time, work on the console of the computer, and
334do
335.Em NOT
336do anything you don't understand.
337.Pp
338When manipulating/adding chain entries, service and protocol names are
339not accepted.
340.Sh AUTHORS
341Ugen J. S. Antsilevich,
342Poul-Henning Kamp,
343Alex Nash,
344Archie Cobbs.
345API based upon code written by Daniel Boulet for BSDI.
346.Sh HISTORY
347.Nm
348first appeared in
349.Fx 2.0 .
350