]> git.saurik.com Git - apple/network_cmds.git/blame - ipfw.tproj/ipfw.8
network_cmds-76.tar.gz
[apple/network_cmds.git] / ipfw.tproj / ipfw.8
CommitLineData
b7080c8e
A
1.\"
2.\" $Id: ipfw.8,v 1.1.1.1 2000/01/11 01:48:49 wsanchez Exp $
3.\"
4.Dd July 20, 1996
5.Dt IPFW 8 SMM
6.Os FreeBSD
7.Sh NAME
8.Nm ipfw
9.Nd controlling utility for IP firewall
10.Sh SYNOPSIS
11.Nm ipfw
12.Op Fl q
13.Oo
14.Fl p Ar preproc
15.Op Fl D Ar macro Ns Op Ns =value
16.Op Fl U Ar macro
17.Oc
18file
19.Nm ipfw
20.Oo
21.Fl f
22|
23.Fl q
24.Oc
25flush
26.Nm ipfw
27.Oo
28.Fl q
29.Oc
30zero
31.Op Ar number ...
32.Nm ipfw
33delete
34.Ar number ...
35.Nm ipfw
36.Op Fl aftN
37list
38.Op Ar number ...
39.Nm ipfw
40.Oo
41.Fl ftN
42.Oc
43show
44.Op Ar number ...
45.Nm ipfw
46.Oo
47.Fl q
48.Oc
49add
50.Op Ar number
51.Ar action
52.Op log
53.Ar proto
54from
55.Ar src
56to
57.Ar dst
58.Op via Ar name | ipno
59.Op Ar options
60.Sh DESCRIPTION
61If used as shown in the first synopsis line, the
62.Ar file
63will be read line by line and applied as arguments to the
64.Nm
65command.
66.Pp
67Optionally, a preprocessor can be specified using
68.Fl p Ar preproc
69where
70.Ar file
71is to be piped through. Useful preprocessors include
72.Xr cpp 1
73and
74.Xr m4 1 .
75If
76.Ar preproc
77doesn't start with a slash as its first character, the usual
78.Ev PATH
79name search is performed. Care should be taken with this in environments
80where not all filesystems are mounted (yet) by the time
81.Nm
82is being run (e. g. since they are mounted over NFS). Once
83.Fl p
84has been specified, optional
85.Fl D
86and
87.Fl U
88specifications can follow and will be passed on to the preprocessor.
89This allows for flexible configuration files (like conditionalizing
90them on the local hostname) and the use of macros to centralize
91frequently required arguments like IP addresses.
92.Pp
93Each packet that has been received or is about to be sent goes through
94the
95.Nm
96rules. In the case of a host acting as a gateway, packets that are
97forwarded by the host are processed by
98.Nm
99twice
100.Po
101once when entering, and once when leaving
102.Pc .
103Each packet can be filtered based on the following information that is
104associated with it:
105.Pp
106.Bl -tag -offset indent -compact -width xxxx
107.It Receive Interface Pq Ar recv
108Interface over which the packet was received
109.It Transmit Interface Pq Ar xmit
110Interface over which the packet would be transmitted
111.It Incoming Pq Ar in
112Packet was just received
113.It Outgoing Pq Ar out
114Packet would be transmitted
115.It Source IP Address
116Sender's IP Address
117.It Destination IP Address
118Target's IP Address
119.It Protocol
120IP protocol, including but not limited to IP
121.Pq Ar ip ,
122UDP
123.Pq Ar udp ,
124TCP
125.Pq Ar tcp ,
126or
127ICMP
128.Pq Ar icmp
129.It Source Port
130Sender's UDP or TCP port
131.It Destination Port
132Target's UDP or TCP port
133.It Connection Setup Flag Pq Ar setup
134This packet is a request to setup a TCP connection
135.It Connection Established Flag Pq Ar established
136This packet is part of an established TCP connection
137.It All TCP Flags Pq Ar tcpflags
138One or more of the TCP flags: close connection
139.Pq Ar fin ,
140open connection
141.Pq Ar syn ,
142reset connection
143.Pq Ar rst ,
144push
145.Pq Ar psh ,
146acknowledgment
147.Pq Ar ack ,
148and
149urgent
150.Pq Ar urg
151.It Fragment Flag Pq Ar frag
152This packet is a fragment of an IP packet
153.It IP Options Pq Ar ipoptions
154One or more of the IP options: strict source route
155.Pq Ar ssrr ,
156loose source route
157.Pq Ar lsrr ,
158record route
159.Pq Ar rr ,
160and timestamp
161.Pq Ar ts
162.It ICMP Types Pq Ar icmptypes
163One or more of the ICMP types: echo reply
164.Pq Ar 0 ,
165destination unreachable
166.Pq Ar 3 ,
167source quench
168.Pq Ar 4 ,
169redirect
170.Pq Ar 5 ,
171echo request
172.Pq Ar 8 ,
173router advertisement
174.Pq Ar 9 ,
175router solicitation
176.Pq Ar 10 ,
177time-to-live exceeded
178.Pq Ar 11 ,
179IP header bad
180.Pq Ar 12 ,
181timestamp request
182.Pq Ar 13 ,
183timestamp reply
184.Pq Ar 14 ,
185information request
186.Pq Ar 15 ,
187information reply
188.Pq Ar 16 ,
189address mask request
190.Pq Ar 17 ,
191and address mask reply
192.Pq Ar 18
193.El
194.Pp
195Note that may be dangerous to filter on the source IP address or
196source TCP/UDP port because either or both could easily be spoofed.
197.Pp
198The
199.Nm
200code works by going through the rule-list for each packet
201until a match is found.
202All rules have two associated counters, a packet count and
203a byte count.
204These counters are updated when a packet matches the rule.
205.Pp
206The rules are ordered by a
207.Dq line-number
208from 1 to 65534 that is used
209to order and delete rules. Rules are tried in increasing order, and the
210first rule that matches a packet applies.
211Multiple rules may share the same number and apply in
212the order in which they were added.
213.Pp
214If a rule is added without a number, it is numbered 100 higher than the highest
215defined rule number, unless the highest defined rule number is 65435 or
216greater, in which case new rules are given that same number.
217.Pp
218The delete operation deletes the first rule with number
219.Ar number ,
220if any.
221.Pp
222The list command prints out the current rule set.
223.Pp
224The show command is equivalent to
225.Sq ipfw -a list .
226.Pp
227The zero operation zeroes the counters associated with rule number
228.Ar number .
229.Pp
230The flush operation removes all rules.
231.Pp
232Any command beginning with a
233.Sq # ,
234or being all blank, is ignored.
235.Pp
236One rule is always present:
237.Bd -literal -offset center
23865535 deny all from any to any
239.Ed
240.Pp
241This rule is the default policy, i.e., don't allow anything at all.
242Your job in setting up rules is to modify this policy to match your
243needs.
244.Pp
245However, if the kernel option
246.Dq IPFIREWALL_DEFAULT_TO_ACCEPT
247is active, the rule is instead:
248.Bd -literal -offset center
24965535 allow all from any to any
250.Ed
251.Pp
252This variation lets everything pass through. This option should only be
253activated in particular circumstances, such as if you use the firewall
254system as an on-demand denial-of-service filter that is normally wide open.
255.Pp
256The following options are available:
257.Bl -tag -width flag
258.It Fl a
259While listing, show counter values. See also
260.Dq show
261command.
262.It Fl f
263Don't ask for confirmation for commands that can cause problems if misused
264(i.e. flush).
265.Ar Note ,
266if there is no tty associated with the process, this is implied.
267.It Fl q
268While adding, zeroing or flushing, be quiet about actions (implies '-f').
269This is useful for adjusting rules by executing multiple
270.Nm
271commands in a script
272.Po
273e.g.,
274.Sq sh /etc/rc.firewall
275.Pc ,
276or by processing a file of many
277.Nm
278rules,
279across a remote login session. If a flush is performed in normal
280(verbose) mode (with the default kernel configuration), it prints a message.
281Because all rules are flushed, the
282message cannot be delivered to the login session. This causes the
283remote login session to be closed and the remainder of the ruleset is
284not processed. Access to the console is required to recover.
285.It Fl t
286While listing, show last match timestamp.
287.It Fl N
288Try to resolve addresses and service names in output.
289.El
290.Pp
291.Ar action :
292.Bl -hang -offset flag -width 1234567890123456
293.It Ar allow
294Allow packets that match rule.
295The search terminates. Aliases are
296.Ar pass ,
297.Ar permit ,
298and
299.Ar accept .
300.It Ar deny
301Discard packets that match this rule.
302The search terminates.
303.Ar Drop
304is an alias for
305.Ar deny .
306.It Ar reject
307(Deprecated.) Discard packets that match this rule, and try to send an ICMP
308host unreachable notice.
309The search terminates.
310.It Ar unreach code
311Discard packets that match this rule, and try to send an ICMP
312unreachable notice with code
313.Ar code ,
314where
315.Ar code
316is a number from zero to 255, or one of these aliases:
317.Ar net ,
318.Ar host ,
319.Ar protocol ,
320.Ar port ,
321.Ar needfrag ,
322.Ar srcfail ,
323.Ar net-unknown ,
324.Ar host-unknown ,
325.Ar isolated ,
326.Ar net-prohib ,
327.Ar host-prohib ,
328.Ar tosnet ,
329.Ar toshost ,
330.Ar filter-prohib ,
331.Ar host-precedence ,
332or
333.Ar precedence-cutoff .
334The search terminates.
335.It Ar reset
336TCP packets only. Discard packets that match this rule,
337and try to send a TCP reset
338.Pq RST
339notice.
340The search terminates.
341.It Ar count
342Update counters for all packets that match rule.
343The search continues with the next rule.
344.It Ar divert port
345Divert packets that match this rule to the
346.Xr divert 4
347socket bound to port
348.Ar port .
349The search terminates.
350.It Ar tee port
351Send a copy of packets matching this rule to the
352.Xr divert 4
353socket bound to port
354.Ar port .
355The search continues with the next rule. This feature is not yet implemeted.
356.It Ar fwd ipaddr Op ,port
357Change the next-hop on matching packets to
358.Ar ipaddr ,
359which can be an IP address in dotted quad or a host name.
360If
361.Ar ipaddr
362is not a directly-reachable address, the route
363as found in the local routing table for that IP is used
364instead.
365If
366.Ar ipaddr
367is a local address, then on a packet entering the system from a remote
368host it will be diverted to
369.Ar port
370on the local machine, keeping the local address of the socket set
371to the original IP address the packet was destined for. This is intended
372for use with transparent proxy servers. If the IP is not
373a local address then the port number (if specified) is ignored and
374the rule only applies to packets leaving the system. This will
375also map addresses to local ports when packets are generated locally.
376The search terminates if this rule matches. If the port number is not
377given then the port number in the packet is used, so that a packet for
378an external machine port Y would be forwarded to local port Y. The kernel
379must have been compiled with optiions IPFIREWALL_FORWARD.
380.It Ar skipto number
381Skip all subsequent rules numbered less than
382.Ar number .
383The search continues with the first rule numbered
384.Ar number
385or higher.
386.El
387.Pp
388If a packet matches more than one
389.Ar divert
390and/or
391.Ar tee
392rule, all but the last are ignored.
393.Pp
394If the kernel was compiled with
395.Dv IPFIREWALL_VERBOSE ,
396then when a packet matches a rule with the
397.Ar log
398keyword a message will be printed on the console.
399If the kernel was compiled with the
400.Dv IPFIREWALL_VERBOSE_LIMIT
401option, then logging will cease after the number of packets
402specified by the option are received for that particular
403chain entry. Logging may then be re-enabled by clearing
404the packet counter for that entry.
405.Pp
406Console logging and the log limit are adjustable dynamically
407through the
408.Xr sysctl 8
409interface.
410.Pp
411.Ar proto :
412.Bl -hang -offset flag -width 1234567890123456
413.It Ar ip
414All packets match. The alias
415.Ar all
416has the same effect.
417.It Ar tcp
418Only TCP packets match.
419.It Ar udp
420Only UDP packets match.
421.It Ar icmp
422Only ICMP packets match.
423.It Ar <number|name>
424Only packets for the specified protocol matches (see
425.Pa /etc/protocols
426for a complete list).
427.El
428.Pp
429.Ar src
430and
431.Ar dst :
432.Bl -hang -offset flag
433.It Ar <address/mask>
434.Op Ar ports
435.El
436.Pp
437The
438.Em <address/mask>
439may be specified as:
440.Bl -hang -offset flag -width 1234567890123456
441.It Ar ipno
442An ipnumber of the form 1.2.3.4.
443Only this exact ip number match the rule.
444.It Ar ipno/bits
445An ipnumber with a mask width of the form 1.2.3.4/24.
446In this case all ip numbers from 1.2.3.0 to 1.2.3.255 will match.
447.It Ar ipno:mask
448An ipnumber with a mask width of the form 1.2.3.4:255.255.240.0.
449In this case all ip numbers from 1.2.0.0 to 1.2.15.255 will match.
450.El
451.Pp
452The sense of the match can be inverted by preceding an address with the
453.Dq not
454modifier, causing all other addresses to be matched instead. This
455does not affect the selection of port numbers.
456.Pp
457With the TCP and UDP protocols, optional
458.Em ports
459may be specified as:
460.Pp
461.Bl -hang -offset flag
462.It Ns {port|port-port} Ns Op ,port Ns Op ,...
463.El
464.Pp
465Service names (from
466.Pa /etc/services )
467may be used instead of numeric port values.
468A range may only be specified as the first value,
469and the length of the port list is limited to
470.Dv IP_FW_MAX_PORTS
471(as defined in
472.Pa /usr/src/sys/netinet/ip_fw.h )
473ports.
474.Pp
475Fragmented packets which have a non-zero offset (i.e. not the first
476fragment) will never match a rule which has one or more port
477specifications. See the
478.Ar frag
479option for details on matching fragmented packets.
480.Pp
481Rules can apply to packets when they are incoming, or outgoing, or both.
482The
483.Ar in
484keyword indicates the rule should only match incoming packets.
485The
486.Ar out
487keyword indicates the rule should only match outgoing packets.
488.Pp
489To match packets going through a certain interface, specify
490the interface using
491.Ar via :
492.Bl -hang -offset flag -width 1234567890123456
493.It Ar via ifX
494Packet must be going through interface
495.Ar ifX.
496.It Ar via if*
497Packet must be going through interface
498.Ar ifX ,
499where X is any unit number.
500.It Ar via any
501Packet must be going through
502.Em some
503interface.
504.It Ar via ipno
505Packet must be going through the interface having IP address
506.Ar ipno .
507.El
508.Pp
509The
510.Ar via
511keyword causes the interface to always be checked.
512If
513.Ar recv
514or
515.Ar xmit
516is used instead of
517.Ar via ,
518then the only receive or transmit interface (respectively) is checked.
519By specifying both, it is possible to match packets based on both receive
520and transmit interface, e.g.:
521.Pp
522.Dl "ipfw add 100 deny ip from any to any out recv ed0 xmit ed1"
523.Pp
524The
525.Ar recv
526interface can be tested on either incoming or outgoing packets, while the
527.Ar xmit
528interface can only be tested on outgoing packets. So
529.Ar out
530is required (and
531.Ar in
532invalid) whenver
533.Ar xmit
534is used. Specifying
535.Ar via
536together with
537.Ar xmit
538or
539.Ar recv
540is invalid.
541.Pp
542A packet may not have a receive or transmit interface: packets originating
543from the local host have no receive interface. while packets destined for
544the local host have no transmit interface.
545.Pp
546Additional
547.Ar options :
548.Bl -hang -offset flag -width 1234567890123456
549.It frag
550Matches if the packet is a fragment and this is not the first fragment
551of the datagram.
552.Ar frag
553may not be used in conjunction with either
554.Ar tcpflags
555or TCP/UDP port specifications.
556.It in
557Matches if this packet was on the way in.
558.It out
559Matches if this packet was on the way out.
560.It ipoptions Ar spec
561Matches if the IP header contains the comma separated list of
562options specified in
563.Ar spec .
564The supported IP options are:
565.Ar ssrr
566(strict source route),
567.Ar lsrr
568(loose source route),
569.Ar rr
570(record packet route), and
571.Ar ts
572(timestamp).
573The absence of a particular option may be denoted
574with a
575.Dq ! .
576.It established
577Matches packets that have the RST or ACK bits set.
578TCP packets only.
579.It setup
580Matches packets that have the SYN bit set but no ACK bit.
581TCP packets only.
582.It tcpflags Ar spec
583Matches if the TCP header contains the comma separated list of
584flags specified in
585.Ar spec .
586The supported TCP flags are:
587.Ar fin ,
588.Ar syn ,
589.Ar rst ,
590.Ar psh ,
591.Ar ack ,
592and
593.Ar urg .
594The absence of a particular flag may be denoted
595with a
596.Dq ! .
597A rule which contains a
598.Ar tcpflags
599specification can never match a fragmented packet which has
600a non-zero offset. See the
601.Ar frag
602option for details on matching fragmented packets.
603.It icmptypes Ar types
604Matches if the ICMP type is in the list
605.Ar types .
606The list may be specified as any combination of ranges
607or individual types separated by commas.
608.El
609.Sh CHECKLIST
610Here are some important points to consider when designing your
611rules:
612.Bl -bullet -hang -offset flag
613.It
614Remember that you filter both packets going in and out.
615Most connections need packets going in both directions.
616.It
617Remember to test very carefully.
618It is a good idea to be near the console when doing this.
619.It
620Don't forget the loopback interface.
621.El
622.Sh FINE POINTS
623There is one kind of packet that the firewall will always discard,
624that is an IP fragment with a fragment offset of one.
625This is a valid packet, but it only has one use, to try to circumvent
626firewalls.
627.Pp
628If you are logged in over a network, loading the KLD version of
629.Nm
630is probably not as straightforward as you would think.
631I recommend this command line:
632.Bd -literal -offset center
633kldload /modules/ipfw.ko && \e
634ipfw add 32000 allow all from any to any
635.Ed
636.Pp
637Along the same lines, doing an
638.Bd -literal -offset center
639ipfw flush
640.Ed
641.Pp
642in similar surroundings is also a bad idea.
643.Pp
644The IP filter list may not be modified if the system security level
645is set to 3 or higher
646.Po
647see
648.Xr init 8
649for information on system security levels
650.Pc .
651.Sh PACKET DIVERSION
652A divert socket bound to the specified port will receive all packets diverted
653to that port; see
654.Xr divert 4 .
655If no socket is bound to the destination port, or if the kernel
656wasn't compiled with divert socket support, diverted packets are dropped.
657.Sh EXAMPLES
658This command adds an entry which denies all tcp packets from
659.Em cracker.evil.org
660to the telnet port of
661.Em wolf.tambov.su
662from being forwarded by the host:
663.Pp
664.Dl ipfw add deny tcp from cracker.evil.org to wolf.tambov.su 23
665.Pp
666This one disallows any connection from the entire crackers network to
667my host:
668.Pp
669.Dl ipfw add deny all from 123.45.67.0/24 to my.host.org
670.Pp
671Here is a good usage of the
672.Ar list
673command to see accounting records
674and timestamp information:
675.Pp
676.Dl ipfw -at l
677.Pp
678or in short form without timestamps:
679.Pp
680.Dl ipfw -a l
681.Pp
682This rule diverts all incoming packets from 192.168.2.0/24 to divert port 5000:
683.Pp
684.Dl ipfw divert 5000 all from 192.168.2.0/24 to any in
685.Sh SEE ALSO
686.Xr cpp 1 ,
687.Xr m4 1 ,
688.Xr divert 4 ,
689.Xr ip 4 ,
690.Xr ipfirewall 4 ,
691.Xr protocols 5 ,
692.Xr services 5 ,
693.Xr init 8 ,
694.Xr kldload 8 ,
695.Xr reboot 8 ,
696.Xr sysctl 8 ,
697.Xr syslogd 8 .
698.Sh BUGS
699.Pp
700.Em WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!
701.Pp
702This program can put your computer in rather unusable state. When
703using it for the first time, work on the console of the computer, and
704do
705.Em NOT
706do anything you don't understand.
707.Pp
708When manipulating/adding chain entries, service and protocol names are
709not accepted.
710.Pp
711Incoming packet fragments diverted by
712.Ar divert
713are reassembled before delivery to the socket, whereas fragments diverted via
714.Ar tee
715are not.
716.Pp
717Port aliases containing dashes cannot be first in a list.
718.Pp
719The
720.Dq tee
721action is unimplemented.
722.Sh AUTHORS
723.An Ugen J. S. Antsilevich ,
724.An Poul-Henning Kamp ,
725.An Alex Nash ,
726.An Archie Cobbs .
727API based upon code written by
728.An Daniel Boulet
729for BSDI.
730.Sh HISTORY
731.Nm
732first appeared in
733.Fx 2.0 .